C# - Nullable types

"In C#, nullable types" are a feature that allows variables, which are like storage boxes for numbers and data, to hold a special "null" value along with their usual values. In C#, value types (e.g., int, double, bool) are typically non-nullable, meaning they must always contain a valid value. But sometimes, you might want to show that something doesn't have a value. That's when nullable types become useful. To make a type nullable, you just add a ? (question mark) at the end of its name.

The nullable types are defined by appending a ? (question mark) to the value type's name. For example:



int? nullableInt;
bool? nullableBool;
float? nullableFloat;
DateTime? nullableDateTime;

Here are some key points to understand about nullable types:

  1. Default value: A nullable type without an assigned value has a default value of null.
  2. HasValue and Value properties: Nullable types have two properties: HasValue and Value. The HasValue property is a boolean that indicates whether the nullable value contains a valid value or is 'null'. The Value property retrieves the actual value if HasValue is true, but accessing Value when HasValue is false will throw an InvalidOperationException.
  3. Coalescing operator (??): The coalescing operator ?? is often used with nullable types to provide a default value if the nullable value is 'null'. For example: 'int x = nullableInt ?? defaultValue;'.
  4. Null-conditional operator (?): The null-conditional operator ?. can be used to safely access members or methods of a nullable type without causing a NullReferenceException. If the nullable value is null, the expression will return null without attempting to access the member or execute the method.

    
    
    int? nullableInt = null;
    int? anotherNullableInt = 42;
    
    if (nullableInt.HasValue)
    {
        Console.WriteLine("nullableInt has a value: " + nullableInt.Value);
    }
    else
    {
        Console.WriteLine("nullableInt is null");
    }
    
    int result = nullableInt ?? 0; // Assign 0 if nullableInt is null
    
    Console.WriteLine("anotherNullableInt: " + anotherNullableInt);
    Console.WriteLine("NullableInt with null-coalescing: " + result);
    
    

    Output:

    1. We declare two nullable integer variables, nullableInt and anotherNullableInt. Nullable integers can hold integer values or be set to null to represent the absence of a value.
    2. We use the HasValue property to check if nullableInt has a value. In this case, it's set to null, so the condition in the if statement is not met. Therefore, the program goes to the else block and prints "nullableInt is null."
    3. We use the null-coalescing operator ?? to assign a default value of 0 to the result variable if nullableInt is null. Since nullableInt is indeed null, result is set to 0.
    4. We print the values of anotherNullableInt and result. anotherNullableInt is explicitly set to 42, so it prints "anotherNullableInt: 42." result is set to 0 due to the null-coalescing operator, so it prints "NullableInt with null-coalescing: 0."

    So, the final output of the program will be:

    
    nullableInt is null
    anotherNullableInt: 42
    NullableInt with null-coalescing: 0
    

    This output demonstrates how nullable types work in C#. The program looks to see if nullableInt has something in it. If it's empty, it uses a default value and then prints both nullable integers values.

    Nullable types are useful when working with databases or APIs that might return or accept null values for certain value types. Nullable types offer a means to manage values that may or may not be present. they allow to differentiate between a valid value and the non-existence of a value.