Null Coalescing Operator (??)

Added in .NET 4.8


Before this operator you had to use the conditional operator.
If "a" is null then (b = -1).
If "a" is not null then (b = a)

int? a = null; 
int? b = (a == null) ? -1 : a;
Console.WriteLine(b);

Alternatively you can use the '??' operator to assign a nullable type to a non-nullable type using slightly less code.

int? a = null; 
int? b = a ?? -1;
Console.WriteLine(b);


Null Coalescing Assignment Operator (??=)

sometimes called the compound assignment operator ?



© 2024 Better Solutions Limited. All Rights Reserved. © 2024 Better Solutions Limited TopPrevNext