Conditional Access expression cannot be assigned - C# null-propagation += events
Asked Answered
D

2

13

One of my favorite C# features added is the "null-propagation" in CS6.

This has cleaned up so much code for many of us.

I came across a situation where this doesn't appear to be possible. I am not sure why as I though the null-propagation was just some compiler magic that does some null checks for us, allowing us to maintain cleaner code.

In the case of hooking into events..

 public override void OnApplyTemplate()
    {
        _eventStatus = base.GetTemplateChild(PART_EventStatus) as ContentControl;

        // This not permitted and will not compile
        _eventStatus?.IsMouseDirectlyOverChanged += EventStatusOnIsMouseDirectlyOverChanged;

        // but this will work
        if(_eventStatus != null) _eventStatus.IsMouseDirectlyOverChanged += EventStatusOnIsMouseDirectlyOverChanged;

        base.OnApplyTemplate();
    }

    private void EventStatusOnIsMouseDirectlyOverChanged(object sender, DependencyPropertyChangedEventArgs dependencyPropertyChangedEventArgs)
    {
        throw new NotImplementedException();
    }

Compile output shows:

 error CS0079: The event 'UIElement.IsMouseDirectlyOverChanged' can only appear on the left hand side of += or -=

Resharper Complaint

So, my question is - what and I misunderstanding about null-propagation? Why is this not a permitted syntax?

Discriminating answered 1/8, 2017 at 22:32 Comment(1)
You can only use it to read; you cannot use it to write. Hence, expression cannot be assigned. I guess one way to think of it is a property without a setter.Presbyterian
H
23

This is by design. The null propagation operator allows to propagate nulls while evaluating an expression, but it cannot be used as the target of an assignment.

Think of it this way: The operator returns a value. However, you need a variable on the left-hand side of the assignment. Having a value there wouldn't make sense.

An issue was opened about this, and is currently open as a feature request. The reply we got at the time was the following:

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

Hanging answered 1/8, 2017 at 23:1 Comment(6)
I am going to list this as the answer, but it looks like maybe some hope in later versions of .net. This was also an interesting SO answer. #32519700Discriminating
This is a good link to subscribe too if anyone is interested. github.com/dotnet/csharplang/issues/737Discriminating
Logically if a value cannot be assigned to it then ... don't assign the value. Exactly how it works when doing accessing. Dumb.Magel
For those still interested, a C# feature proposal has recently been championed regarding this operator: github.com/dotnet/csharplang/issues/2883. Will see if it gets picked up.Hanging
there is a "new" feature request for this: github.com/dotnet/csharplang/issues/6045Gramineous
and a proposal: github.com/dotnet/csharplang/blob/main/proposals/…Chinkapin
L
2

Expression (which null propagation operator always returns) can not be used as left part of assignment.

Lysippus answered 1/8, 2017 at 22:53 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.