Sunday, 14 May 2017

Null-conditional operators in C# 6.0

Null-Conditional operator enable developers to check the null value with in an object reference chain. The null – conditional operator ( ?.) , returns null if anything in the object reference chain in null. This avoid checking null for each and every nested objects if they are all in referenced and having null values.
The expression A?.B evaluates to B if the left operand (A) is non-null; otherwise, it evaluates to null.
1
2
3
4
5
6
public void NullConditionalOPerator()
{
    string name = null;
    var length = name?.Length;
    Console.WriteLine(length);
}

No comments:

Post a Comment