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();
}
}



Manish Kumar ChoudharyPosted Jan 8, 2015, 1:48 PM
Nice one...
Imran AhmedPosted Jan 8, 2015, 7:35 AM
good one Rizwan Ali
Praveen KumarPosted Jan 8, 2015, 6:11 AM
Nice Rizwan Ali
Rizwan AliPosted Jan 8, 2015, 1:20 AM
yes you are right sir
Rahul BansalPosted Jan 8, 2015, 12:55 AM
Nice Start but both codes (old and new) are same. Might be in new method, Console.WriteLine(a.age?.no1 ?? 10001 ); will be use as you mentioned earlier in the article.