In this blog, I am going to share with you about Null Coalescing Operator (??) in C#.
Null Coalescing Operator (??)
- A syntax used to check null and assign a default value if null
- This operator can be used with Nullable types as well as reference types.
- This operator can be represented as x ?? y.
Here, x and y are the left and right operands. The left operand (x) can be nullable type while the right operand should have non-nullable value. If the value of x is null, then the syntax returns the value of y; else the value of x will be returned. This means that the value of left operand will be returned if its value is not null; else the value of right operand will be returned.
Why should we use Null Coalescing Operator (??)?
There are numerous of uses/advantages of Null Coalescing Operator,
It helps in preventing InvalidOperationException.
When we run the below program then 'a.Value' throw an InvalidOperationException exception as "An unhandled exception of type 'System.InvalidOperationException' occurred in mscorlib.dll. Additional information: Nullable object must have a value."
- using System;
-
- namespace Tutpoint
- {
- class Program
- {
- static void Main(string[] args)
- {
- int? a = null;
- int? b = a.Value;
-
- Console.ReadKey();
-
- }
- }
- }
This can be overcome by null coalescing operator as shown in below program. 'b' is assigned with default value 33 as 'a' is null.
- using System;
-
- namespace Tutpoint
- {
- class Program
- {
- static void Main(string[] args)
- {
- int? a = null;
- int? b = a ?? 33;
- Console.WriteLine("Value of a= " + a + " and b= " + b);
- Console.ReadKey();
-
- }
- }
- }
Output
Value of a= and b= 33
It helps in defining a default value for a nullable item as we did in above program.(assigning a default value 33)
It removes many redundant "if-else" conditions and makes the code more organized and readable.
- using System;
-
- namespace Tutpoint
- {
- class Program
- {
- static void Main(string[] args)
- {
- int? a = null;
-
- int? b;
- if (a.HasValue)
- {
- b = a;
- }
- else
- {
- b = 33;
- }
- Console.WriteLine("Value of a= " + a + " and b= " + b);
- Console.ReadKey();
-
- }
- }
- }
The above code can be re-written using null coalescing as shown below,
- using System;
-
namespace Tutpointorganized- class Program
- {
- static void Main(string[] args)
- {
- int? a = null;
-
- int? b = a ?? 33;
-
- Console.WriteLine("Value of a= " + a + " and b= " + b);
- Console.ReadKey();
-
- }
- }
- }
The program above is looking clean, organized and easily readable. It minimises the use of if-else conditions in one line. Also, the number of line of codes decrease. The output is same for both programs.
Output
Value of a= and b= 33
Null coalescing works for both reference-types and value-types.
- using System;
-
- namespace Tutpoint
- {
- class Program
- {
- static void Main(string[] args)
- {
-
- string str1 = null;
- string str2 = "Hello";
- string str3 = "Buddy";
-
- string str4 = str1 ?? str2;
- str3 = str4 ?? str2;
-
-
- int? a = null;
- bool? c = null;
- Program p = new Program();
- bool? b = c ?? p.IsValue(11, 33);
-
- Console.WriteLine("str3= " + str3 + " , str4= " + str4 + " b=" + b);
- Console.ReadKey();
-
- }
- public Nullable<bool> IsValue(int? num1, int? num2)
- {
- try
- {
- if (num1.HasValue && num2.HasValue)
- {
- return true;
- }
- else
- return false;
-
- }
- catch (Exception)
- {
- return null;
- }
- }
- }
-
- }
In the above program, we have shown how to use both reference type as well as value type. We can also use a method as an operand on null coalescing syntax.
In line bool? b = c ?? p.IsValue(11, 33);
value of c is null so the value of right operand will be evaluated. As both the parameter's value is not null, IsValue(11,13) will return true and true will be assigned to b.
This operator can be nested, which looks like a chain.
Null coalescing can be nested to make a chain like structure as shown below in the program. This also reduces the if-else condition statements and make the code more readable and easy to understand.
- using System;
-
- namespace Tutpoint
- {
- class Program
- {
- static void Main(string[] args)
- {
- int? num1 = null;
- int? num2 = null;
- int? num3 = 3;
- int? num4 = num1 ?? num2 ?? num3;
-
- Console.WriteLine("Value of num4 = " + num4);
- Console.ReadKey();
-
- }
- }
-
- }
Output
Value of num4 = 3
Conclusion
Null coalescing should be incorporated inside code to minimise the line of code and to standardized the code. This does not make much improvement on the performance but it optimise the code. This makes the code easy to understand.
Thank you. Please feel free to ask any question or make a suggestion.