Null-Coalescing Assignment Operator in C# 8.0
C# 8.0 has introduced a new operator that is known as a Null-coalescing assignment operator(??=). This operator is used to assign the value of its right-hand operand to its left-hand operand, only if the value of the left-hand operand is null. If the left-hand operand evaluates to non-null, then this operator does not evaluate its right-hand operand.
Syntax:
p ??= q
Here, p is the left and q is the right operand of ??= operator. If the value of p is null, then ??= operator assigns the value of q in p. Or if the value of p is non-null, then it does not evaluate q.
Important Points:
- The left-hand operand of the ??= operator must be a variable, or a property, or an indexer element.
- It is right-associative.
- You cannot overload ??= operator.
- You are allowed to use ??= operator with reference types and value types.
Example:
// C# program to illustrate how to use// ??= operator with value types and// reference typesusingSystem;namespaceexample {classGFG {staticvoidMain(string[] args){// Reference typesstringitem_1 =null;stringitem_2 =null;stringitem_4 ="GeeksforGeeks";stringitem_5 ="GFG";// Using ??= operatoritem_1 = item_1 ??= item_4;stringitem_6 = item_2 ??= item_5;Console.WriteLine("Value of item_1 is: {0}\n"+"Value of item_6 is:{1}", item_1, item_6);// Value typesint? ele_1 =null;GFG obj =newGFG();// Using ??= operator assigns// the value of ele_1// And also you are allowed to// use method with ??= operatorele_1 = ele_1 ??= obj.Add(10, 30);Console.WriteLine("Value of ele_1 is {0}", ele_1);}// MethodpublicintAdd(inta,intb){intresult = a + b;returnresult;}}}Output:
Value of item_1 is: GeeksforGeeks Value of item_6 is:GFG Value of ele_1 is 40
- With the help of ??= operator you can remove many redundant “if-else” conditions and make your code more compact and readable. As shown in the below example:
Example:
// C# program to illustrate how to use// ??= operator to remove if statementsusingSystem;namespaceexample {classGFG {// Main MethodstaticvoidMain(string[] args){// Nullable variableint? element =null;// Checking the element is null or notif(elementisnull) {// Assign a new value to the element// Using ??= operatorint? new_element = element ??= 400;Console.WriteLine("Element is null. "+"So the new element is: {0}", new_element);}}}}Output:
Element is null. So the new element is: 400
// C# program to illustrate how to use// ??= operator to remove if statementsusingSystem;namespaceexample {classProgram {staticvoidMain(string[] args){// Nullable variableint? element =null;// Using ??= operator// Assign values to the null variableelement ??= 400;Console.WriteLine("Element is: {0}", element);}}}Output:
Element is: 400
- You can also use ??= operator like a nested chain. It makes your code more readable.
Example:
// C# program to illustrate// how to nest ??= operatorusingSystem;namespaceexample {classGFG {// Main MethodstaticvoidMain(string[] args){// Nullable variablesint? ele1 =null;int? ele2 =null;int? ele3 = 45;// Using Nested ??= operatorint? result = ele1 ??= ele2 ??= ele3;Console.WriteLine("Element is: {0}", result);}}}Output:
Element is: 45


