If the left-hand operand is not null, the null-coalescing operator?? returns its value; otherwise, it evaluates the right-hand operand and returns its result. If the left-hand operand evaluates to non-null, the?? operator does not evaluate the right-hand operand.
The null-coalescing assignment operator??=, which is available in C# 8.0 and later, assigns the value of its right-hand operand to its left-hand operand only if the left-hand operand evaluates to null. If the left-hand operand evaluates to non-null, the??= operator does not evaluate the right-hand operand.
namespace NullCoalescing
{
class Program
{
static int Main(string[] args)
{
int? ticketAvailable = 100;
int TotalTicket;
if (ticketAvailable == null)
{
TotalTicket = 0;
}
else
{
TotalTicket = ticketAvailable.Value;
}
Console.WriteLine(TotalTicket);
//Example of null Coalescing operator
TotalTicket = ticketAvailable ?? 200;
Console.WriteLine(TotalTicket);
return 0;
}
}
}