#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

About Sean
Software developer in the Twin Cities area, passionate about software development and sailing.

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 comment