-
Notifications
You must be signed in to change notification settings - Fork 4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Proposal: C# null conditional assignment #3366
Comments
I'm trying to understand why this would be a good thing. |
It would be similar to the ?. operator in that it reduces the verbosity of the code, so it's advantages are similar. Admittedly, it is probably a less frequent operation. A disadvantage, compared with the null conditional operator, is that is less clear which variable is checked for null. |
I can see some value in an operator like this, though I'm not sure that it brings clarity of any kind. Basically, the proposal is for a null-coalescing-assignment operator. It does reduce some verbosity in some cases, but there's a ton of holes in the current design like: how do you know which choice it made? It seems to a case for simply coalescing the following statement into a single statement:
into
While I suppose I could see uses for this, I'm not it makes for overly clear code. Additionally, in the flow of things I don't have a way to know if the value of
This is even more concise and is fairly explicit in that the value after the |
Would it make a lot more sense to allow
|
@paulomorgado interesting idea, but I don't think that fulfills the contract of the null-coalescing-assignment which is to say "assign a value but if it is null, use this default value instead". The |
That kind of is different than everything already in C#. Isn't it? |
With the introduction of the null conditional operator it might make sense to also add null conditional assignment, for instance like this:
Which would be functional equivalent to:
The text was updated successfully, but these errors were encountered: