#776 – Declaring and Using Nullable structs

You can make any value type nullable, allowing it to either be null or to contain one of its normal values.

Since user-defined structs are value types, you can also make any struct nullable, using Nullable<T> or T? notation.

            // Regular struct
            GuyInfo gi1 = new GuyInfo("Isaac", 1642);

            // Nullable, with no value
            Nullable<GuyInfo> gi2 = null;

            // Nullable, 2nd form, with value
            GuyInfo? gi3 = new GuyInfo("Albert", 1879);

            bool hasVal1 = gi2.HasValue;
            bool hasVal2 = gi3.HasValue;

776-001

Advertisements

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 #776 – Declaring and Using Nullable structs

  1. Pingback: Dew Drop – February 8, 2013 (#1,496) | Alvin Ashcraft's Morning Dew

  2. Pingback: Interesting .NET Links – February 11 , 2013 | TechBlog

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 )

Google+ photo

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

Connecting to %s

%d bloggers like this: