Use nullable parameters in C#

nullable parameters

This example shows how you can use nullable parameters to simulate optional parameters. The example Use null to simulate optional parameters in C# explains how you can pass null to a method to simulate optional parameters, but that only works for parameters that are reference types. Value types, such as int and bool, cannot take the value null, so you cannot pass null for parameters of those types.

To work around this restriction, you can use nullable types. Using a special syntax, you can turn a value type into a new type that can be null. To do this, add a question mark after the parameter’s type as in the following declaration.

int? num_points;

To see if a nullable variable contains a value, use its HasValue property. Then if the variable has a value, use its Value property to get that value.

The following DrawStar method draws a star.

// Draw a star.
private void DrawStar(Graphics gr, int? point_count,
    int? skip_count, Rectangle bounds, Pen pen, Brush brush)
    int num_points = (point_count.HasValue) ? point_count.Value : 5;
    int num_skip = (skip_count.HasValue) ? skip_count.Value : 2;

    // Get the star's points.
    PointF[] points = MakeStarPoints(
        -Math.PI / 2, num_points, num_skip, bounds);

    // Fill the star.
    if (brush != null) gr.FillPolygon(brush, points);

    // Draw the star.
    if (pen != null) gr.DrawPolygon(pen, points);

The point_count, skip_count, pen, and brush parameters can all be null. The pen and brush parameters can be null because they are object references and those can always be null. The point_count and skip_count parameters are nullable integers.

Most of the parameters are self-explanatory. The main exception is the skip_count parameter. It represents the number of points on the outside of the star the program should skip when connecting star points. For example, a normal five-pointed star connects every second point so skip_count is 2. (If you set skip_count to 1, you get a regular polygon. For a five-pointed star, a pentagon.)

The bounds parameter is a Rectangle. That’s a structure so it cannot be null. In this case that makes sense because there really isn’t a reasonable default value for the star’s location.

The method uses the conditional operator ?: to set num_points equal to the point_count parameter is it is present and the default value 5 if point_count is null. Similarly it sets num_skip to skip_count or the default value 2, depending on whether skip_count is null.

The method then calls the MakeStarPoint method to generate the points that make up the star. See the post Draw a star with a given number of points in C# for a description of that method.

Finally the fills and outlines the star if the brush and pen parameters are present.

Download Example   Follow me on Twitter   RSS feed

This entry was posted in OOP, syntax and tagged , , , , , , , , , , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *