C# language feature:

  • Nullable types

applies to

  • value types (bool, datetime, enum, struct, etc)

benefit

  • Useful when dealing with values from a database, when values returned from your table is NULL.

how to use

  • Add ? after the value type at declaration.

note

  • Reference types are already nullable.

example:

    class Nullable_Type_Example
    {
        int? number { get; set; } //  add`?` after value type

        void Set2null()
        {
            number = null;  // now this won't generate compiler error
        }
    }