[Proposal] Null-coalescing Assignment Operator: ??=
See original GitHub issue??=
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 ??=
.
Issue Analytics
- State:
- Created 9 years ago
- Reactions:5
- Comments:17 (9 by maintainers)
Top Results From Across the Web
Null coalescing assignment - C# 8.0 draft feature ...
This proposal adds a non-overloadable binary operator to the language that performs this function. There have been at least eight separate ...
Read more >c# - The behavior of null-coalescing assignment operator
C# 8.0 introduces the null-coalescing assignment operator ??=. You can use the ??= operator to assign the value of its right-hand operand to...
Read more >Proposal: null-coalescing assignment operator ??= #13737
Proposal I'd like to propose an idea for next C#, with name: null-coalescing assignment operator We can combine the ?? and = operators...
Read more >PHP RFC: Null Coalescing Assignment Operator
??= operator is an assignment operator. If the left parameter is null, assigns the value of the right paramater to the left one....
Read more >Null coalescing operator
The null coalescing operator is a binary operator that is part of the syntax for a basic conditional expression in several programming languages, ......
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Given the amount of interest in it (5 linked items) I’ve decided to try making a prototype – which is currently at https://github.com/ashmind/roslyn/tree/features/coalesce-assignment. That’s my first attempt at a Roslyn thing, so I’m sure I’m doing everything wrong.
I’ve also added that branch to TryRoslyn: http://tryroslyn.azurewebsites.net/#b:ashmind-features-coalesce-assignment/K4Zwlgdg5gBAygTxAFwKYFsDcAoADsAIwBswBjGUogQxBBgGEYBvbGNmFAJ0lgA8d2HZN2gwEA9q3b5iZGADcA9mAAmMALIAKAJTMpgtrxgBeGACJeZiQZgB6WzAAWyZLhAAue1DDJHhAHSkiui2KorIEKjItpyKIEQIELZgtMCoILYATAAMAKz6BggwAPzFpvwFAL7YlUA=
The benefits of this proposal do not overcome the drawbacks in terms of language complexity. It is unlikely to ever have a high enough benefit-for-cost to make us prefer it over many other things we could do in this release or many releases into the future.