Introduction
Null coalescing operator(??) is one of the really great feature of C#
language . This operator (??) is used to check whether object is null or not .
If the object is null it provides a default value. Most of the programmer didn't use this coalescing operator in frequent programming practices.
We can say that coalescing operator (??) is used to
define a default value for nullable value types or reference types. When a nullable type is
assigned to a non-nullable type the ?? operator defines the default value to
be returned. If a programmer try to assign a nullable value type to a
non-nullable value type and he/she not using the ?? operator then a
compile time error will be generated .
Example:
using System;
namespace
coalescingOperatorDemo
{
class Program
{
static void Main()
{
Console.WriteLine(N);
N = 1;
Console.WriteLine(N);
Console.ReadKey();
}
public static int? _num;
public static int? N
{
get
{
return
_num ?? 0;
}
set
{
_num = value;
}
}
}
}