Operator ??=

Topics: C# Language Design
May 15, 2014 at 12:46 PM
Operator ??= work like other similar 'op'= operators:
A.B.C ??= d;
expanded to
A.B.C = A.B.C ?? d;
or to
var temp = A.B;
temp.C = temp.C ?? d;
May 15, 2014 at 1:39 PM
One potential difficulty with such an operator is that it wouldn't be obvious whether it should use CompareExchange. There are many cases where a field should be written exactly once over the lifetime of an object, and such a write should use CompareExchange, but use of that method when not needed could represent a performance cost.