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

Open
SLaks opened this Issue Mar 15, 2015 · 14 comments

Comments

Projects
None yet
9 participants
@SLaks
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

This comment has been minimized.

Show comment
Hide comment
@DavidKarlas

DavidKarlas Mar 26, 2015

Contributor

Bump, Is this bug or?

Contributor

DavidKarlas commented Mar 26, 2015

Bump, Is this bug or?

@gafter

This comment has been minimized.

Show comment
Hide comment
@gafter

gafter Mar 26, 2015

Member

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

Member

gafter commented Mar 26, 2015

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

@gafter gafter closed this Mar 26, 2015

@gafter gafter self-assigned this Mar 26, 2015

@SLaks SLaks changed the title from Null-propagation operator cannot be used with event accessors? to Feature request: Allow null-propagation operator for event accessors Mar 26, 2015

@SLaks

This comment has been minimized.

Show comment
Hide comment
@SLaks

SLaks Mar 26, 2015

Contributor

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

Contributor

SLaks commented Mar 26, 2015

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

@DavidKarlas

This comment has been minimized.

Show comment
Hide comment
@DavidKarlas

DavidKarlas Mar 26, 2015

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

Contributor

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

@gafter gafter reopened this 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

This comment has been minimized.

Show comment
Hide comment
@SLaks

SLaks Mar 26, 2015

Contributor

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

Contributor

SLaks commented Mar 26, 2015

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

@series0ne

This comment has been minimized.

Show comment
Hide comment
@series0ne

series0ne Sep 22, 2015

@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!

@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

This comment has been minimized.

Show comment
Hide comment
@jnm2

jnm2 Sep 22, 2016

Contributor

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.

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

This comment has been minimized.

Show comment
Hide comment
@jnm2

jnm2 Sep 22, 2016

Contributor

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

Contributor

jnm2 commented Sep 22, 2016

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

@miloush

This comment has been minimized.

Show comment
Hide comment
@miloush

miloush 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?

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

This comment has been minimized.

Show comment
Hide comment
@salmelo

salmelo 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.

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

This comment has been minimized.

Show comment
Hide comment
@SLaks

SLaks Nov 15, 2016

Contributor

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

Contributor

SLaks commented Nov 15, 2016

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

@alrz alrz referenced this issue in dotnet/csharplang Mar 23, 2017

Open

Champion "Null-coalescing assignments" #34

2 of 5 tasks complete

@jnm2 jnm2 referenced this issue in dotnet/csharplang Apr 15, 2017

Closed

Discosal: Null-conditional return #437

@jnm2

This comment has been minimized.

Show comment
Hide comment
@jnm2

jnm2 Apr 15, 2017

Contributor

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

Contributor

jnm2 commented Apr 15, 2017

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

@Misiu

This comment has been minimized.

Show comment
Hide comment
@Misiu

Misiu 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.

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

This comment has been minimized.

Show comment
Hide comment
@jnm2

jnm2 Jul 12, 2017

Contributor

Issue moved to dotnet/csharplang #737 via ZenHub

Contributor

jnm2 commented Jul 12, 2017

Issue moved to dotnet/csharplang #737 via ZenHub

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