Null Coalescing Operator (??) in C#

The .NET Framework provides the Null Coalescing Operator (??) which is a kind of binary operator and enables the developers to quickly check for the null values .

The Null Coalescing Operator (??) in C# can be used with Nullable types as well as reference types .

For example , a??b .If a is not null then assign the value a else take the value b.

Null Coalescing Operator (??) in C#

Below is a sample sourecode demonstrating the usage of Null Coalescing Operator (??) in C#

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

namespace ConsoleApplication1

{

internal class Program

{

// Null Coalescing Operator (??) in C#

private static void Main(string[] args)

{

int? IntValue = null;

int output = IntValue ?? -1;

Console.WriteLine("Result = " + output.ToString());

Console.ReadLine();

}

}

}
Null Coalescing Operator (??) in C#