#207 – Nullable Types

Because value types can’t normally represent null values, C# includes nullable types–types that can represent their normal range of values or represent a null value.

Any value type can be used as a nullable type by adding a trailing ? to the type name.

            int i = 12;   // regular int, can't be null

            int? j = 22;  // Nullable int, can be null
            j = null;     // Can also be null

Here are some other examples of nullable types.  In each case, we can set the variable’s value to null, which means that the variable doesn’t have a value that falls within the range of the corresponding type.

            double? r = null;
            bool? thisIsFalse = null;
            Mood? myMood = null;

About Sean
Software developer in the Twin Cities area, passionate about .NET technologies. Equally passionate about my own personal projects related to family history and preservation of family stories and photos.

2 Responses to #207 – Nullable Types

  1. Pingback: #566 – Implicit Conversions to Nullable Types « 2,000 Things You Should Know About C#

  2. Pingback: #581 – Boxing and Unboxing Nullable Types « 2,000 Things You Should Know About C#

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

Join 43 other followers