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#
C#
x
37
37
1
using System;
2
3
using System.Collections.Generic;
4
5
using System.Linq;
6
7
using System.Text;
8
9
using System.Threading.Tasks;
10
11
namespace ConsoleApplication1
12
13
{
14
15
internal class Program
16
17
{
18
19
// Null Coalescing Operator (??) in C#
20
21
private static void Main(string[] args)
22
23
{
24
25
int? IntValue = null;
26
27
int output = IntValue ?? -1;
28
29
Console.WriteLine("Result = " + output.ToString());
30
31
Console.ReadLine();
32
33
}
34
35
}
36
37
}
Leave Your Comment