Null coalescing assignment
The null coalescing operator, ??
, has been extended in C# 8 to support assignment. A popular usage for the null coalescing operator involves the parameter checks at the beginning of a method, like in the following example:
class Person { public Person(string firstName, string lastName, int age) { this.FirstName = firstName ?? throw new ArgumentNullException(nameof(firstName)); this.LastName = lastName ?? throw new ArgumentNullException(nameof(lastName)); this.Age = age; } public string FirstName { get; set; } public string LastName { get; set; } public int Age { get; set; } }
The new assignment allows us to reassign the reference whenever it is null, as demonstrated...