0

As we know,Null-Conditional Operator is something new in c# 6.0(https://msdn.microsoft.com/en-us/magazine/dn802602.aspx). And I tried to use it in WPF(.net framework 4.6.1).

Here is my code which is right:

PackageButtonClass PBC = PackageButtonList.Find(x => x.Index == index + 1);
            if (PBC != null)
            {
                PBC.IsEnabled = false;
            }



I wanna use Null-Conditional Operator to make it short,like this:

PackageButtonList.Find(x => x.Index == index + 1)?.IsEnabled = false;



However,WPF throw an error which is 'Left-Hand Side Of An Assignment Must Be a Variable, Property or Indexer'

What 's wrong with my code?Would you please help me?Thank you.

Melon NG
  • 2,568
  • 6
  • 27
  • 52
  • 1
    Question: In all of those samples, do you see any assignments? Also, this is not a WPF question. – ProgrammingLlama Jan 10 '18 at 13:51
  • @john you showed maybe is what I want,thank you. – Melon NG Jan 10 '18 at 13:53
  • You can make an extension method, similar to [ForEach](https://stackoverflow.com/a/1509463/1997232) with delegate as parameter, to execute delegate when value isn't null, if you want one-liner. – Sinatr Jan 10 '18 at 14:04

3 Answers3

2
PackageButtonList.Find(x => x.Index == index + 1)?.IsEnabled = false;

Consider what would happen if the index was not found? Then you're trying to assign to a property that is not there.

Thus this usage of ?. is not supported.

Richard
  • 106,783
  • 21
  • 203
  • 265
  • I wanna to do that:If index was not found,then do nothing.If index was found,then change the property. – Melon NG Jan 10 '18 at 13:55
  • By the way,is there any other way to make these code for short? – Melon NG Jan 10 '18 at 13:57
  • @MelonNG I don't think there is (except by having a helper method taking a lambda which does the assignment. But that's worse (and likely to be confusing) than just putting the `if` in. – Richard Jan 10 '18 at 14:00
1

This has nothing to do with WPF.
You can't use the null conditional operator as the left hand side of an assignment expression, and the reason is painfully obvious:

When you write a code line like this: Instance?.Property = value;, and the Instance is null, Instance?.Property would evaluate to null, so you would have a meaningless code line stating null = value.

This is why the compiler would not allow that. If you want to avoid a null reference exception in such cases, you have to do it the old-fashion way:

if(Instance != null)
{
    Instance.Property = Value;
}

You could write it in a single line of code, but I personally find this less readable:

if(Instance != null) Instance.Property = Value;

A slightly more readable code would be two lines of code:

if(Instance != null) 
    Instance.Property = Value;
Zohar Peled
  • 79,642
  • 10
  • 69
  • 121
0

An expression using ?. is never an lvalue - that is to say, a value you can assign to.

If it were it would have a condition underwhich that lvalue would have to write to a null reference's property or field, which would result in a NullReferenceException.

Logically, either this can happen, in which case you should guard against it by only doing the operation inside a branch on a test of it, or it can never happen, in which case the ?. would be better expressed as .

Jon Hanna
  • 110,372
  • 10
  • 146
  • 251