Null Propagation Operator in C#
Jan 28, 2020 • 5 Minute Read
Introduction
Back in 2016, Microsoft held an event called Visual Studio Connect() where they released the preview version of Visual Studio 2015, and C# 6.0. This new version incorporated many changes that developers and the technology of that time required.
Just to mention a few of the new features:
- Read-only auto-properties
- Auto-property initializers
- Expression-bodied function members
- Using static
- Null-conditional operators
For a full list, see here.
In this guide, we will take a look at the null-conditional operators, more commonly referred to as null-propagation operators. There are also folks out there who refer to this feature as safe-navigation operator.
The Operators
In order to understand, we need to introduce two new operators. The main goal of these operators is to make code execution more fluid and replace the NullReferenceExeption with meaningful error handling.
- ?. -> Called member access operator
- ?[] -> Called element access operator
Let's look at an example of the member access operator. Before C#, the below code would have resulted in System.NullReferenceException: 'Object reference not set to an instance of an object.'
using System;
namespace Pluralsight
{
public class NullProp
{
public static void Main()
{
Server DC = new Server();
Console.WriteLine(DC.name.Length);
Console.ReadKey();
}
class Server
{
public string name { get; set; }
}
}
}
The problem here is that we would like to check the length of a property that was never initialized, and this throws the exception.
One way to handle this is with the following modification of the code. We would like to wrap the critical code in a try - catch block looking for null references.
try
{ Console.WriteLine(DC.name.Length); }
catch(System.NullReferenceException)
{ Console.WriteLine("The name is not initialized."); }
This gives us the following output.
The name is not initialized.
This seems to do the trick, however, we need to modify the code as follows.
Console.WriteLine(DC?.name ?? "No Name provided yet!");
If we do not initialize the name property beforehand, we get the following output.
No Name provided yet!
This saves us the try - catch block and reduces the code length. The ?? operator is called the null-coalescing operator, which allows us to write conditional expressions which return a default value when the expression evaluates to null. The ?. can be used to invoke a delegate in a thread-safe manner, which helps the development of multithreaded applications.
Note: This operator's nickname is the Elvis operator.
Let's see an example for the element access operator. We would like to modify the above code as follows.
using System;
namespace Pluralsight
{
public class NullProp
{
public static void Main()
{
Server[] servers = new Server[5];
Server S1 = new Server();
S1.name = "First";
Server S2 = new Server();
S2.name = "Second";
Server S3 = new Server();
Server S4 = new Server();
S4.name = "First";
Server S5 = new Server();
servers[0] = S1;
servers[1] = S2;
servers[2] = S3;
servers[3] = S4;
servers[4] = S5;
foreach(Server s in servers)
{
Console.WriteLine(s?.name ?? "No Name specified!");
}
Console.ReadKey();
}
class Server
{
public string name { get; set; }
}
}
}
We need an array of five elements. This array will be filled with servers, and some servers will have their name properties initialized. Then we iterate over the servers and check with the member access operator if the servers' names are initialized. Okay, now you may be wondering where the demonstration is of the element access operator. The for loop can be easily converted to this type.
for(int i = 0; i < 5; i++)
{
Console.WriteLine(servers?[i].name ?? "No name was specified!");
}
We can even combine the element and member access operators this way.
for(int i = 0; i < 5; i++)
{
Console.WriteLine(servers?[i]?.name ?? "No name was specified!");
}
The following output is produced when we execute the code.
# first for loop
First
Second
No Name specified!
First
No Name specified!
# second for loop
First
Second
No name was specified!
First
No name was specified!
Conclusion
The null-propagation operator represents a breaking point that brought resiliency to the C# programming language. It allows you to handle the System.NullReferenceException exception in a more elegant way. It also reduces the number of lines of code that need to be written and helps you avoid bugs further on. All in all, it makes code cleaner and easier to maintain. I hope this was informative to you, and I would like to thank you for reading it.