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 types
using
System;
namespace
example {
class
GFG {
static
void
Main(
string
[] args)
{
// Reference types
string
item_1 =
null
;
string
item_2 =
null
;
string
item_4 =
"GeeksforGeeks"
;
string
item_5 =
"GFG"
;
// Using ??= operator
item_1 = item_1 ??= item_4;
string
item_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 types
int
? ele_1 =
null
;
GFG obj =
new
GFG();
// Using ??= operator assigns
// the value of ele_1
// And also you are allowed to
// use method with ??= operator
ele_1 = ele_1 ??= obj.Add(10, 30);
Console.WriteLine(
"Value of ele_1 is {0}"
, ele_1);
}
// Method
public
int
Add(
int
a,
int
b)
{
int
result = a + b;
return
result;
}
}
}
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 statements
using
System;
namespace
example {
class
GFG {
// Main Method
static
void
Main(
string
[] args)
{
// Nullable variable
int
? element =
null
;
// Checking the element is null or not
if
(element
is
null
) {
// Assign a new value to the element
// Using ??= operator
int
? 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 statements
using
System;
namespace
example {
class
Program {
static
void
Main(
string
[] args)
{
// Nullable variable
int
? element =
null
;
// Using ??= operator
// Assign values to the null variable
element ??= 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 ??= operator
using
System;
namespace
example {
class
GFG {
// Main Method
static
void
Main(
string
[] args)
{
// Nullable variables
int
? ele1 =
null
;
int
? ele2 =
null
;
int
? ele3 = 45;
// Using Nested ??= operator
int
? result = ele1 ??= ele2 ??= ele3;
Console.WriteLine(
"Element is: {0}"
, result);
}
}
}
Output:
Element is: 45