Null-Coalescing Operator in C#
In C#, ?? operator is known as Null-coalescing operator. It will return the value of its left-hand operand if it is not null. If it is null, then it will evaluate the right-hand operand and returns its result. Or if the left-hand operand evaluates to non-null, then it does not evaluate its right-hand operand.
Syntax:
p ?? q
Here, p is the left and q is the right operand of ?? operator. The value of p can be nullable type, but the value of q must be non-nullable type. If the value of p is null, then it returns the value of q. Otherwise, it will return the value of p.
Important Points:
- The ?? operator is used to check null values and you can also assign a default value to a variable whose value is null(or nullable type).
- You are not allowed to overload ?? operator.
- It is right-associative.
- In ?? operator, you can use throw expression as a right-hand operand of ?? operator which makes your code more concise.
- You are allowed to use ?? operator with value types and reference types.
Example:
// C# program to illustrate how to use// ?? operator with value types and// reference typesusingSystem;namespaceexample {classProgram {staticvoidMain(string[] args){// Reference typesstringitem_1 =null;stringitem_2 ="GeeksforGeeks";stringitem_3 ="GFG";stringitem_4 = item_1 ?? item_2;item_3 = item_4 ?? item_2;Console.WriteLine("Value of item_4 is: {0} \n"+"Value of item_3 is: {1}", item_4, item_3);// Value typesint? item_5 =null;Program obj =newProgram();// Using ?? operator assigns// the value of a value type// and also you are allowed// to use method with ?? operatorint? item_6 = item_5 ?? obj.Add(10, 30);Console.WriteLine("Value of item_6 is: {0}", item_6);}// MethodpublicintAdd(inta,intb){intresult = a + b;returnresult;}}}chevron_rightfilter_noneOutput:
Value of item_4 is: GeeksforGeeks Value of item_3 is: GeeksforGeeks Value of item_6 is: 40
- With the help of ?? operator you can prevent InvalidOperationException.
Example:
// C# program to illustrate how ??// operator prevent the// InvalidOperationExceptionusingSystem;namespaceexample {classGFG {// Main MethodstaticvoidMain(string[] args){// Creating items of nullable typesint? item_1 =null;/*Here if you use this commented part,then this statement will give you anInvalidOperationException. So toovercome this problem we use ?? operatorint? item_2 = item_1.Value;*/// With the help of ?? operator we// assign a default value to the item_2// And the value of item_1 is null.int? item_2 = item_1 ?? 100;Console.WriteLine("Value of item_1 is: {0}", item_1);Console.WriteLine("Value of item_2 is: {0}", item_2);}}}chevron_rightfilter_noneOutput:
Value of item_1 is: Value of item_2 is: 100
- With the help of ?? operator you can remove many redundant “if-else” conditions and make your code compact and readable.
Example:
// C# program to illustrate how ??// operator replaces if-else statementsusingSystem;namespaceexample {classGFG {// Main MethodstaticvoidMain(string[] args){// Creating items of nullable typesint? item_1 =null;int? item_2;if(item_1.HasValue) {item_2 = item_1;}else{item_2 = 200;}Console.WriteLine("Value of item_1 is: {0}", item_1);Console.WriteLine("Value of item_2 is: {0}", item_2);}}}chevron_rightfilter_noneOutput:
Value of item_1 is: Value of item_2 is: 200
// C# program to illustrate how ??// operator replaces if-else statementsusingSystem;namespaceexample {classGFG {// Main MethodstaticvoidMain(string[] args){// Creating items of nullable typesint? item_1 =null;// Using ?? operatorint? item_2 = item_1 ?? 200;Console.WriteLine("Value of item_1 is: {0}", item_1);Console.WriteLine("Value of item_2 is: {0}", item_2);}}}chevron_rightfilter_noneOutput:
Value of item_1 is: Value of item_2 is: 200
- ?? operator can be nested. It will make your code more readable and also reduce multiple if-else conditions.
Example:
// C# program to illustrate how// we use nested ?? operatorusingSystem;namespaceexample {classGFG {// Main MethodstaticvoidMain(string[] args){// Creating items of nullable typesint? item_1 =null;int? item_2 =null;int? item_3 = 500;// Nested ?? operatorint? item_4 = item_1 ?? item_2 ?? item_3;Console.WriteLine("Value of item_4 is: {0} ", item_4);}}}chevron_rightfilter_noneOutput:
Value of item_4 is: 500
Recommended Posts:
- Null-Coalescing Assignment Operator in C# 8.0
- C# | Operator Overloading
- LINQ | Filtering Operator | OfType
- C# | as Operator Keyword
- C# | is Operator Keyword
- Is vs As operator keyword in C#
- typeof Operator Keyword in C#
- LINQ | Set Operator | Except
- LINQ | Element Operator | Last
- LINQ | Projection Operator | Select
- LINQ | Projection Operator | SelectMany
- LINQ | Filtering Operator | where
- LINQ | Sorting Operator | OrderBy
- LINQ | Sorting Operator | OrderByDescending
- LINQ | Sorting Operator | Reverse
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

