


operator: It's how you access the indexer on an array, or a class that implements an indexer. The ? syntax has the same semantics as the ?. We could write this statement: var thisName = people?.FirstName Now, we have a couple of levels of member access to navigate, and one of those levels uses the indexer syntax ( ). Suppose people is a variable that represents an IList. Code Cleanup with the Null Conditional Operator The power of this feature come from all the scenarios where this feature enables cleaner code. If p is non-null, age is the wrapped value of p.Age. If p is null, age is an int? with no value. As with name, the value of age depends on the value of p. The variable age is an int? (which is another way of specifying a Nullable).

Note that p.FirstName may be null even when p is not. If p is not null, name is the value of p.FirstName. The value of name depends on the value of p. Consider these two statements: var name = p?.FirstName Consider this simplified Person class: public class PersonĪssume that p represents a person. Let's look at some examples to explain those behaviors. operators, knowing that the first null encountered prevents the remaining (rightmost) components of the expression from being evaluated.

One of the most versatile and useful additions to the C# language in version 6 is the null conditional operator. C# Async Fundamentals LiveLessons (Video Training), Downloadable
