-
Notifications
You must be signed in to change notification settings - Fork 4.2k
Closed
Labels
Area-Language DesignFeature RequestLanguage-C#Resolution-Won't FixA real bug, but Triage feels that the issue is not impactful enough to spend time on.A real bug, but Triage feels that the issue is not impactful enough to spend time on.
Description
??= would have semantics analogous to the existing compound assignment operators like +=, %=, and <<=. More precisely, x ??= y would be equivalent to x = x ?? y except that x is evaluated only once. It would have clear, if not vast, utility. Hopefully, it would not be an unreasonably large effort.
It is perhaps debatable whether
object o = GetFirstChoice( foo, bar, someLongArgument )
?? GetSecondChoice( foo, bar, someLongArgument )
?? GetFallback();looks better than
object o = GetFirstChoice( foo, bar, someLongArgument );
o ??= GetSecondChoice( foo, bar, someLongArgument );
o ??= GetFallback();However, the latter seems easier to step through in the debugger.
Furthermore, a use case like
private static void ApplyNewSettings( SettingsSource newSource ) {
// Set each setting if and only if it is not already set to a non-null value.
// This could be e.g. applying defaults after deserializing user settings.
this.SomeSetting ??= newSource.SomeSetting;
// ...
}is less repetitive with ??=.
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
Area-Language DesignFeature RequestLanguage-C#Resolution-Won't FixA real bug, but Triage feels that the issue is not impactful enough to spend time on.A real bug, but Triage feels that the issue is not impactful enough to spend time on.