Skip to content
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

Closed
SLaks opened this issue Mar 15, 2015 · 16 comments
Closed

Feature request: Allow null-propagation operator for event accessors #1276

SLaks opened this issue Mar 15, 2015 · 16 comments

Comments

@SLaks
Copy link
Contributor

SLaks commented Mar 15, 2015

Why can't I write code like this?

Process.GetProcessById(2)?.Exited += delegate { };

Error:

CS0079 The event 'Process.Exited' can only appear on the left hand side of += or -=

This would be especially useful when writing property setters that add and remove event handlers to their values if non-null.

@DavidKarlas
Copy link
Contributor

Bump, Is this bug or?

@gafter
Copy link
Member

gafter commented Mar 26, 2015

the ?. Operator never produces an lvalue, so this is by design.

@gafter gafter closed this as completed Mar 26, 2015
@gafter gafter added the Resolution-Answered The question has been answered label Mar 26, 2015
@gafter gafter self-assigned this Mar 26, 2015
@SLaks SLaks changed the title Null-propagation operator cannot be used with event accessors? Feature request: Allow null-propagation operator for event accessors Mar 26, 2015
@SLaks
Copy link
Contributor Author

SLaks commented Mar 26, 2015

Can this be reopened as a feature request?
This would be very useful for code like this.

@DavidKarlas
Copy link
Contributor

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 ;)

@gafter gafter reopened this Mar 26, 2015
@gafter gafter added Feature Request and removed Question Resolution-Answered The question has been answered labels Mar 26, 2015
@gafter gafter added this to the C# 7 and VB 15 milestone Mar 26, 2015
@gafter gafter assigned anatay and unassigned gafter and anatay Mar 26, 2015
@SLaks
Copy link
Contributor Author

SLaks commented Mar 26, 2015

Note that the delegate expression (eg, x?.SomeEvent += ExpensiveFunction().SomeMethod) would still be evaluated if x is null.

@MrMatthewLayton
Copy link

@SLaks you're not the only one that has raised this:

http://stackoverflow.com/questions/32519200/c-sharp-6-0-null-propagation-operator-property-assignment/32715351#32715351

Congrats for also adding delegates/events to the mix; I never considered this!

@jnm2
Copy link
Contributor

jnm2 commented 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
Copy link
Contributor

jnm2 commented Sep 22, 2016

Just for context, I ran into wanting this again today.

@miloush
Copy link

miloush commented 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;

@SLaks

Note that the delegate expression (eg, x?.SomeEvent += ExpensiveFunction().SomeMethod) would still be evaluated if x is null.

Not if it was rewritten to if (x != null) x.SomeEvent += ... or am I missing something? Do you want it to be evaluated?

@salmelo
Copy link

salmelo commented Oct 20, 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 if statement necessary to produce the desired effect now.

@SLaks
Copy link
Contributor Author

SLaks commented Nov 15, 2016

@miloush: You're right; it should check the LHS first.

@jnm2
Copy link
Contributor

jnm2 commented Apr 15, 2017

@SLaks Could you migrate this to https://github.com/dotnet/csharplang please?

@Misiu
Copy link

Misiu commented Jun 7, 2017

Whats the status of this?

I'd like to do this:

TabListControl?.SelectedIndexChanged += this.OnSelectedIndexChanged;

but I must add extra check

if (TabListControl != null)
{
  TabListControl.SelectedIndexChanged += this.OnSelectedIndexChanged;
}

I've looked at Null-coalescing assignments, but this isn't something I could use in this specific case.

@jnm2
Copy link
Contributor

jnm2 commented Jul 12, 2017

Issue moved to dotnet/csharplang #737 via ZenHub

@CyrusNajmabadi
Copy link
Member

Closing out as this is a language request. I believe @RikkiGibson is championing allowing exactly this for the next version of C#.

@CyrusNajmabadi CyrusNajmabadi closed this as not planned Won't fix, can't repro, duplicate, stale Oct 28, 2022
@RikkiGibson
Copy link
Contributor

dotnet/csharplang#6045

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests