-
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
Feature request: Allow null-propagation operator for event accessors #1276
Comments
Bump, Is this bug or? |
the ?. Operator never produces an lvalue, so this is by design. |
Can this be reopened as a feature request? |
Maybe add also properties setters into request like: Object?.Prop = false; I think it's very good feature request compared to some futuristic requests for C#7 ;) |
|
@SLaks you're not the only one that has raised this: Congrats for also adding delegates/events to the mix; I never considered this! |
What would it take to not evaluate the RHS if the object is null? That would seem far more helpful at first blush. It makes me happy that this is being considered. I've wished for this on a regular basis writing UI code. |
Just for context, I ran into wanting this again today. |
Just for the record, I expected this to work for attaching event handlers too, though I now understand what the issue is. object o = null;
INotifyCollectionChanged c = null;
c?.CollectionChanged += OnChanged;
(o as INotifyCollectionChanged)?.CollectionChanged += OnChanged;
((INotifyCollectionChanged)o)?.CollectionChanged += OnChanged;
Not if it was rewritten to |
Would just like to add a vote to this (and null conditional lvalues in general) for whatever that's worth. I run into wanting this very frequently when writing code to bridge UI and internal objects. Edit: Ideally I would want the right hand side not to be evaluated at all in the case of null, much as if you had written the |
@miloush: You're right; it should check the LHS first. |
@SLaks Could you migrate this to https://github.com/dotnet/csharplang please? |
Whats the status of this? I'd like to do this:
but I must add extra check
I've looked at Null-coalescing assignments, but this isn't something I could use in this specific case. |
Issue moved to dotnet/csharplang #737 via ZenHub |
Closing out as this is a language request. I believe @RikkiGibson is championing allowing exactly this for the next version of C#. |
Why can't I write code like this?
Error:
This would be especially useful when writing property setters that add and remove event handlers to their values if non-null.
The text was updated successfully, but these errors were encountered: