12

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?

TravisWhidden
  • 2,142
  • 1
  • 19
  • 43
  • 5
    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. – TyCobb Aug 01 '17 at 22:41

2 Answers2

22

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.

stelioslogothetis
  • 9,371
  • 3
  • 28
  • 53
  • 1
    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. https://stackoverflow.com/questions/32519200/c-sharp-6-0-null-propagation-operator-property-assignment/ – TravisWhidden Aug 01 '17 at 23:06
  • 1
    This is a good link to subscribe too if anyone is interested. https://github.com/dotnet/csharplang/issues/737 – TravisWhidden Aug 01 '17 at 23:10
  • Logically if a value cannot be assigned to it then ... don't assign the value. Exactly how it works when doing accessing. Dumb. – The Muffin Man Mar 31 '20 at 20:54
  • 2
    For those still interested, a C# feature proposal has recently been championed regarding this operator: https://github.com/dotnet/csharplang/issues/2883. Will see if it gets picked up. – stelioslogothetis Dec 24 '21 at 16:27
  • 1
    there is a "new" feature request for this: https://github.com/dotnet/csharplang/issues/6045 – Puschie Jan 02 '23 at 13:33
  • and a proposal: https://github.com/dotnet/csharplang/blob/main/proposals/null-conditional-assignment.md – Patrick McDonald Aug 28 '23 at 14:56
2

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