Null-conditional operators - C# 6 Language New features

csharp-6-new-features-null-conditional-operators
To check null every time while performing any operation is big headache for all developers. Though previous null conditional operator is good at it's own level but still we need some powerful feature that directly check an object or reference data type is null or not. To fulfill this requirement Microsoft released new improved feature Null conditional operators in c# 6.
Null conditional operators is used to check either entity object is null or not before use it's properties. Instead of checking object is null or not in if-else block statement you can just use ? (question mark) to use it. In this article I will use both older approach and new null conditional operator approach to check entity object is null or not, so you'll be able to higher perceive advantage of this new feature.
To test null conditional operator I have make two classes Employee and Address. and make Address class navigation property in Employee class.
public class Employee
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public Address Address { get; set; }
}

public class Address
{
    public string State{ get; set; }
    public string City { get; set; }
}

In previous versions 

If we take about previous version of C#, there's no inbuilt function to be had for checking entity object null or not. We must check employee and employee.Address is null or not, before we use State property in any function.
if (employee!= null && employee.Address != null)
{
    Console.WriteLine(employee.Address.State);
}

Using null conditional operators

Null conditional operators is pretty much easier and simple feature in C# 6 that helps to check object is null or not in same line. In our example employee.Address? will check whether Address is null or not. If it's not null then and then it will retrieve State and City property values.
Employee employee = new Employee()
{
    FirstName = "Bhavik",
    LastName = "Patel",
    Address = new Address() { State = "Guj", City = "Ahd"}
};

Console.WriteLine("State Name: {0}, City Name: {1}",employee.Address?.State,employee.Address?.City);
Console.ReadLine();
Output
The output is all known. It will print appropriate state and city name. 

what if we are not initializing Address class

The big question is what if Address class is not initialized? The answer is employee.Address is null so State and City considered as null and it will simply print blank instead of any value. Though Address class is null compiler will not throw any NullReferenceException.
Employee employee = new Employee()
{
    FirstName = "Bhavik",
    LastName = "Patel"
//  Address = new Address() { State = "Guj", City = "Ahd"}
};

Console.WriteLine("State Name: {0}, City Name: {1}",employee.Address?.State,employee.Address?.City);
Console.ReadLine();
Output

Can we use it on more then that?

Null-conditional operator is not working only fields and properties, you can use it on methods also.
I add method called TrackMe() in Address class.
public void TrackMe()
{
    Console.WriteLine("I am tracking you");
}
use it like
employee.Address?.TrackMe();
Output
I am tracking you
It will print message if object is not null.
Reference