The ?? operator is also known as the null-coalescing operator. It returns the left side operand if the operand is not null else it returns the right side operand. This article explains what is nullable types, the null coalescing operator, and unique ways to use the null-coalescing operator in C#.
- C# Nullables Types
- What Is Null Coalescing Operator In C#?
- Null Coalescing Operators In Practical Scenarios
- Further Reading
A reference type can have an actual value of null, meaning it has no value. A value type can’t have a value of null.
For example, how would you express that some boolean value is true, false, or unknown? Regular Boolean can be only true or false.
This is why Nullables were added to the .Net Framework. A Nullable is a wrapper around a value type with a Boolean flag that it stores if the Nullable has a value set
Below is a simplified version of the support of Nullables in the .NET Framework
You can use the ‘?’ operator to shorthand the syntax e.g. int?, long? instead of using Nullable
Shorthand Syntax For Nullable Types
From the above definition, you can easily make out that:
type is also a value type.
- Nullable Type is of struct type that holds a value type (struct) and a Boolean flag, named HasValue, to indicate whether the value is null or not.
- Since Nullable
itself is a value type, it is fairly lightweight. The size of Nullable type instance is the same as the size of containing value type plus the size of a boolean.
- The nullable types parameter T is struct. i.e., you can use nullable type only with value types. This is quite ok because reference types can already be null. You can also use the Nullable
type for your user defined struct.
- Nullable type is not an extension in all the value types. It is a struct which contains a generic value type and a boolean flag.
This is all about Nullable types in C#. Now I am going to discuss the Null Coalescing operator in C#.
What Is Null Coalescing Operator In C#?
The ?? operator is called the null-coalescing operator. You can use it to provide a default value for nullable value types or for reference types. The operator returns the left value if it’s not null; otherwise, the right operand. Here is an example of using the operator.
In this case, the value of y is -1 because x is null. You can also nest the null-coalescing operator, below is an example,
Of course, you can achieve the same with an if statement but the null-coalescing operator can shorten your code and improve its readability.
Null Coalescing Operators In Practical Scenarios
Let us see how to use the Null coalescing operators in practical scenarios.
Scenario 1 - Assign a Nullable type to Non-Nullable Type
Consider the following piece of code where we are assigning a nullable type to a non-nullable type.
In the code above, if the nullable type (in our case ‘a’) has a null value and the null value is assigned to a non-nullable type (in our case ‘b’), an exception of type InvalidOperationException is thrown.
One way to resolve this error is to use an “IF..ELSE” condition and check the value of the nullable type before assigning it to the non-nullable type
The code will now compile and give you desired results. However using the null coalescing operator in such scenarios, you can create clearer code than the equivalent if-else statement, as shown below:
In the code shown above, if ‘a’ has been assigned a non-null value, then this value will be assigned to the int b. However since the nullable type ‘a’ has been assigned null, the value to the right of the operator (??) i.e. zero will be assigned to b instead.
Scenario 2 - How you can use this operator in LINQ.
Scenario 3 - In expressions with the null-conditional operators ?. and ?
You can use the null-coalescing operator to provide an alternative expression to evaluate in case the result of the expression with null-conditional operations is null:
Scenario 4 - Null-conditional delegate invocation
Use the ?. operator to check if a delegate is non-null and invoke it in a thread-safe way (for example, when you raise an event), as the following code shows:
Scenario 5 - You can use the null coalesce operator to lazy load certain properties.
?. in C#: When properties might be null by Bill Wagner - In this article, Bill explains concise syntax for checking against null, and taking some default action based on the ‘null-ness’ of a variable.
Eliminate null-checks using arrays by Kasper B. Graversen - In this article, Kasper show a coding pattern which eliminate the need for null-checking. since null typically is used to indicate that “nothing is here” - this is essentially the semantics of an empty array. The result is shorter and easier to read code.
Subscribe to Code with Shadman
Get the latest posts delivered right to your inbox