Null-Conditional Operators in C# 6
The null-conditional operators allow developers to access members and elements only when the receiver is not null; otherwise they provide null results. Example:
int? length1 = items?.Length; // null if items is null.
Item first = items?[0]; // null if items is null.
int length2 = items?.Length ?? 0; // "??" returns 0 if items is null.
int? second = items?[1].Orders.Count(); // null if items is null.
int? third = items?[2].Orders?.Count(); // null if items OR Orders is null.
The null-conditional operators allow developers to access members and elements only when the receiver is not null; otherwise they provide null results. Example:
int? length1 = items?.Length; // null if items is null.
Item first = items?[0]; // null if items is null.
int length2 = items?.Length ?? 0; // "??" returns 0 if items is null.
int? second = items?[1].Orders.Count(); // null if items is null.
int? third = items?[2].Orders?.Count(); // null if items OR Orders is null.
No comments:
Post a Comment