C# Generics Nullable Types


Generics – Nullable Types

Generics—Nullable Types
static void Main(string[] args)
{
    System.Nullable<int> nullableInt;
    nullableInt = null;
    if (nullableInt == null) { WriteLine("nullableInt is null"); }
    else { WriteLine("nullableInt is not null"); }
    if (nullableInt.HasValue) { WriteLine("nullableInt is not null"); }
    else { WriteLine("nullableInt is null"); }
    nullableInt = 4;
    if (nullableInt == null) { WriteLine("nullableInt is null"); }
    else { WriteLine("nullableInt is not null"); }
    if (nullableInt.HasValue) { WriteLine("nullableInt is not null"); }
    else { WriteLine("nullableInt is null"); }
    // int? nullableInt is shorthand for System.Nullable<int>
    int? nullableInt2 = 8;
    nullableInt2 = 5;
    ReadKey();
}

Leave a comment

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