In C #, the compiler does not allow you to assign a null value to a variable. Therefore, C # 2.0 provides special functions for assigning a null value to a variable known as a Nullable type. With help of nullable, we can assign a null value to a variable. Types that accept Null values introduced in C # 2.0 can only work with a value type, not a reference type.
Null acceptance types for a reference type are introduced later in C # 8.0 in 2019 so that we can explicitly define whether a reference type can contain a null value. This helped us fix the NullReferenceException problem without using any conditions. This article describes null-possible types for value types.
The Nullable type is an instance of the System. Nullable structure. Here is a T type that contains value types that cannot be allowed, such as an integer, a floating-point type, a Boolean type, and so on. For example, in a null integer type, you can store values between -2147483648 and 2147483647 or a null value.
Nullable<int> num = null;
Console.WriteLine(num.GetValueOrDefault());
int? num1 = null;
Console.WriteLine(num1.GetValueOrDefault());
0
0
A null coalescing operator, in C #, is an operator used to check whether the value of a variable is zero. In C# null coalescing operator is denoted by the symbol "??".
In the above syntax, a is the right operand and b is the left operand in the coalescing operator.
Some Important Points to remember about the null coalescing operator.
int? a = null;
int? b = 100;
int? c = a ?? b;
Console.WriteLine("Value of c is: {0} ", c);
Value of c is: 100