-
@SLaks commented on Sun Mar 15 2015 Why can't I write code like this? Process.GetProcessById(2)?.Exited += delegate { }; Error:
This would be especially useful when writing property setters that add and remove event handlers to their values if non-null. @DavidKarlas commented on Thu Mar 26 2015 Bump, Is this bug or? @gafter commented on Thu Mar 26 2015 the ?. Operator never produces an lvalue, so this is by design. @SLaks commented on Thu Mar 26 2015 Can this be reopened as a feature request? @DavidKarlas commented on Thu Mar 26 2015 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 commented on Tue Nov 15 2016
@series0ne commented on Tue Sep 22 2015 @SLaks you're not the only one that has raised this: Congrats for also adding delegates/events to the mix; I never considered this! @jnm2 commented on Thu Sep 22 2016 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. @jnm2 commented on Thu Sep 22 2016 Just for context, I ran into wanting this again today. @miloush commented on Wed Oct 12 2016 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 @salmelo commented on Wed Oct 19 2016 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 @SLaks commented on Tue Nov 15 2016 @miloush: You're right; it should check the LHS first. @jnm2 commented on Sat Apr 15 2017 @SLaks Could you migrate this to https://github.com/dotnet/csharplang please? @Misiu commented on Wed Jun 07 2017 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. |
Beta Was this translation helpful? Give feedback.
Replies: 4 comments
-
Left-hand null checkers would be both intuitive and wonderful. There's no reason in 2019 that null checking needs to be so bloaty for such a common task. |
Beta Was this translation helpful? Give feedback.
-
Any updates on this? 🤞 |
Beta Was this translation helpful? Give feedback.
-
@CyrusNajmabadi decided to champion #2883 and believes that this falls under that scope as well as the other kinds of assignments (simple or compound) where the LHS contains a null-propagation operator. |
Beta Was this translation helpful? Give feedback.
-
Currently part of #6045 |
Beta Was this translation helpful? Give feedback.
Currently part of #6045