Back to: C#.NET Tutorials For Beginners and Professionals
Null-Coalescing Assignment Operator in C# 8
In this article, I am going to discuss Null-Coalescing Assignment Operator in C# 8 with Examples. Please read our previous article where we discussed Indices and Ranges in C# 8 with Examples. C# 8.0 has introduced a new operator that is known as a Null-Coalescing Assignment Operator (??=).
Null-Coalescing Assignment Operator (??=) in C# 8
C# 8.0 introduces the null-coalescing assignment operator ??=. We can use this ??= operator to assign the value of its right-hand operand to its left-hand operand only if the left-hand operand evaluates to null. That means the null-coalescing assignment operator ??= assigns a variable only if it is null. The syntax is given below.
a ??= b;
Here, a is the left and b is the right operand of the null-coalescing operator ??=. If the value of a is null, then the ??= operator assigns the value of b in a. If the value of a is not-null, then it does not evaluate b.
It simplifies a common coding pattern where a variable is assigned with a value if it is null. For a better understanding, please have a look at the below diagram. Here, you can observe before C# 8, how we are checking null and assigning a value if it is null and how we can achieve the same in C# 8 using the null-coalescing assignment (??=) operator.
Points to Remember While working with ??= in C#:
- The left-hand operand of the ??= operator must be a variable, a property, or an indexer element.
- It is right-associative.
- You cannot overload ??= operator.
- You are allowed to use the ??= operator with reference types and value types.
Null-Coalescing Assignment Operator Example in C#:
Let us see an example for a better understanding. The following example is self-explained, so please go through the comment lines for a better understanding.
using System; using System.Collections.Generic; using System.Threading.Tasks; namespace Csharp8Features { class NullCoalescingAssignment { static void Main(string[] args) { //Creating null variables List<int>? numbersList = null; int? number = null; //The numbersList is null, so will create new List<int>() object numbersList ??= new List<int>(); //As number is null, so it will assign 25 to number and store it in the list numbersList.Add(number ??= 25); //Now number is not null, so it will not assign 30 to number, //so the previous value 25 will store again in the list numbersList.Add(number ??= 30); foreach (var item in numbersList) { Console.Write(item + " "); // output:25 25 } Console.WriteLine(); Console.WriteLine(number); // output:25 } } }
Output:
Real-Time Use Cases of Null-Coalescing Assignment Operator
With the help of Null-Coalescing Assignment ??= Operator, we can remove many redundant if-else statements and make our code more readable and understandable. Let us understand this with an example. Here, first, I will show you an example using the if statement, and then I will convert the same example using Null-Coalescing Assignment ??= Operator so that you will get a better idea.
Example using If Statements:
using System; using System.Threading.Tasks; namespace Csharp8Features { class NullCoalescingAssignment { static async Task Main(string[] args) { // C# 1..7 int? Age = null; if (Age == null) { Age = 20; } Console.WriteLine(Age); } } }
Same Example using Null-Coalescing Assignment ??= Operator:
using System; using System.Threading.Tasks; namespace Csharp8Features { class NullCoalescingAssignment { static async Task Main(string[] args) { // C# 8 int? Age = null; Age ??= 20; Console.WriteLine(Age); } } }
In the next article, I am going to discuss Unmanaged Constructed Types in C# 8 with Examples. Here, in this article, I try to explain Null-Coalescing Assignment in C# 8 with Examples. I hope you enjoy this Null-Coalescing Assignment in C# with Examples article.