Introduction
In the field of computer science, Microsoft has achieved new targets every day and makes new things for users. These days Microsoft has introduced a new version of Visual Studio. The new version of Visual Studio is named Visual Studio 2015 Preview. Microsoft also introduced the new version of C#, C# 6.0. In this new version of C# Microsoft has added new features. Using these features, this language will be stronger, more powerful, and easy to use. Some features are String Interpolation, nameof operator, #pragma, null propagation operator, Expression bodied members, using static members, Auto-property initializers, Await in catch/finally, Exception filters, and many more, as we know C# 6.0 is not yet complete so it will be premature to say that the features are finalized. There are no changes done by Microsoft to make C# 6.0 better, so working on it is in a running state.
Null Reference Exception in C# 6.0
So here is an explanation of the Null propagation operator in C# 6.0, which is a very useful operator in C# 6.0. It is also known as the Null conditional operator. The main purpose of the null propagation operator is to check the null values because most programmers forget to check out the null values during coding. Sometimes when programmers do not check for a null in the coding, they get the exception "System.NullReferenceException," also known as the Object reference not set to an instance of an object error. The main cause of this exception is that the programmer tried to access a method or member that is not present in the code or maybe is null, so the compiler sends a null reference exception.
So first, here we see an example of how a Null Reference Exception can exist in our code.
Example
using System;
class Program
{
static void Main()
{
string str = null; // Here we declare the string as null
if (str.Length == 0) // By this line, the error will occur
{
Console.WriteLine(str); // Due to the exception, the compiler never goes to this line
}
Console.Read();
}
}
When we run this code, it generates an exception known as a null reference exception, just as in the following image.
Output
Now I will tell you how to remove this exception. First, I made the code proper to remove this error. That means we provide a value for the string and then run it. I think then it will work properly. Let us see it.
There are two ways; one is to first pass a value for the String.
Example
using System;
class Program
{
static void Main()
{
string str = "rizwan ali mansoori";
if (str.Length != 0)
{
Console.WriteLine("Now we provide the value for the string, and it is not null");
Console.WriteLine("so the output is " + str);
}
Console.Read();
}
}
Output
The second way is to use try-and-catch blocks to handle an exception. Now see in the following example how to handle an exception using try and catch blocks.
Example
using System;
class Program
{
static void Main()
{
try
{
string str = null; // Here we declare the string as null
if (str.Length == 0) // By this line, the error will occur
{
}
}
catch (Exception ex)
{
Console.WriteLine("Please provide some value for the string");
}
Console.Read();
}
}
Output
This is the overall information about the Null Reference Exception, what is it, how it can exist, how to handle it in our code, and what the reasons are for it. The end user never wants to see the abnormal termination of his code. He always wants a specific message or warning when he gets errors, so it is necessary for every programmer to please check for the null value of every member that we use in our code.
Null Propagation Operator(?) in C# 6.0
As we said above, C# 6.0 introduced the null propagation operator for checking for null values in code. The null propagation operator is denoted by "?". The basic advantage of using the null propagation operator is to reduce the code we write to check the null condition in our code. Using this operator, we can have less code than the previous code. Using the null propagation operator, we can easily manage the null reference exception.
Before we write the code, something like this.
Console.WriteLine(a.age == null ? 10001 : a.age.no1);
But now we write it in a minimized way like this.
Console.WriteLine(a.age?.no1 ?? 10001);
Here I provide an example of the Null propagation operator.
Example
using System;
namespace ConsoleApplication5
{
class Program
{
static void Main(string[] args)
{
var a = new first
{
number = 1234
};
Console.WriteLine(a.age.no1);
Console.Read();
}
}
class first
{
public int number { get; set; }
public second age { get; set; }
}
class second
{
public int no1 { get; set; }
public int no2 { get; set; }
}
}
It provides the null reference exception like the output.
Output
Now we handle this exception by the old method.
Code of the old method in C# 6.0
using System;
namespace ConsoleApplication5
{
class Program
{
static void Main(string[] args)
{
var a = new first
{
number = 1234
};
Console.WriteLine(a.age == null ? 10001 : a.age.no1); // old method
Console.Read();
}
}
class first
{
public int number { get; set; }
public second age { get; set; }
}
class second
{
public int no1 { get; set; }
public int no2 { get; set; }
}
}
Output
Now finally, we remove this exception using the null propagation operator and reduce the code. If we do not specify null, then we check for null only by the "?" operator.
Code of new method in C# 6.0
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication5
{
class Program
{
static void Main(string[] args)
{
var a = new first
{
number = 1234
};
Console.WriteLine(a.age?.no1 ?? 10001); // new code
Console.Read();
}
}
class first
{
public int number { get; set; }
public second age { get; set; }
}
class second
{
public int no1 { get; set; }
public int no2 { get; set; }
}
}
Output
Summary
C# 6.0 has many new features in it. One of them is the null propagation operator. It has the property that if we make a reference variable in our code and want to check its value for null or not null before invoking the object using the null propagation operator, we can check it and remove the null exception error. It also increases the reliability of code and reduces the lines of code. That makes the code cleaner and easier.