-2

I just noticed that property assignment is not allowed in combination with the null conditional operator. With methods is it not a problem at all though.

I always thought that properties are just syntactic sugar for setter methods.

item?.SetMarker(Marker.Start); // Perfectly fine
item?.Marker = Marker.Start; // Error   CS0131  The left-hand side of an assignment must be a variable, property or indexer

So why is one allowed and the other not?

I'm pretty sure there is a good theoretical reason. With the small hope of getting smarter I just try to know why :)

PS - I noticed the same behaviour in TypeScript.

Dirk Boer
  • 8,522
  • 13
  • 63
  • 111
  • 3
    In the first line of code, you're conditionally calling a method. In the second example, you're definitely doing an assignment to something that might be null. Those are 2 very different things. – itsme86 Jan 13 '21 at 23:01
  • 3
    because how do you assign something to ```null``` ? if ```item``` is null then your line reads: ```null = Marker.Start; ``` ... yeah our logical mind would say, in that case just do nothing about it .... but thats not how the compiler handles it :-) – Tomek Jan 13 '21 at 23:02
  • well I *would* expect the same as with calling the method - you don't do anything when it's null - but I might be overlooking something. – Dirk Boer Jan 13 '21 at 23:02
  • @00110001 what would be the difference with `A?.B?.SetC(D)` ? – Dirk Boer Jan 13 '21 at 23:03
  • @00110001 if A or B is null it wouldn't do anything - exact same behaviour as `A?.B?.SetC(D)` - what is the difference? – Dirk Boer Jan 13 '21 at 23:05
  • 3
    There is a github conversation on this, ill try and find it – TheGeneral Jan 13 '21 at 23:06
  • When calling a method yo ucall it on the instance of the object ... if its null, there is no instance so nothing to call ... when you try to assign something you expect to assign to an instance and not to null and @00110001 is right it is ambigious if you do A?.B?.C = D and especially if A and B and D are totally different classes / types – Tomek Jan 13 '21 at 23:07
  • 2
    Sorry, after taking larger sips of my coffee and thinking about this a little more, the question i posed is not really the problem, and this could be done within reason, however there are other issues to solve. You can read through them here... https://github.com/dotnet/csharplang/issues/2883 and follow the previous discussions – TheGeneral Jan 13 '21 at 23:19
  • In practice, that case is so rare that there are more chance it would be a mistake rom the programmer. Also it is somewhat counter-intuitive as usually right hand side of equal operator is executed first. Thus it might cause more confusion than benefit. – Phil1970 Jan 14 '21 at 00:10
  • `well I would expect the same as with calling the method - you don't do anything when it's null` So on the left side of an assignment acts one way (does nothing), and on the right side it acts a different way (returns null)? _Not saying that is a slam dunk argument - but it would need to be explained to the programmer._ – mjwills Jan 14 '21 at 00:15
  • `item?.SetMarker(Marker.Start); // Perfectly fine item?.Marker = Marker.Start; // Error CS0131 The left-hand side of an assignment must be a variable, property or indexer` Even if you allowed the second syntax it wouldn't be exactly equivalent either I suspect. Assignments (in the latter code) are evaluated by calculating the right side and then assigning to the left - so the `Marker.Start` would be evaluated all of the time. But the former code would evaluate `Marker.Start` only if `item` was not null (which means the behaviour of the two will differ if `Marker.Start` threw an exception). – mjwills Jan 14 '21 at 00:19
  • From the answer to the duplicate: _"the result of Null conditional operator is always a value and you can't assign a value to a value, hence the error"_ – Peter Duniho Jan 14 '21 at 02:47

1 Answers1

0

In the second line, when item is null, it does not have any Marker, so you are not able to assign any value to it.

On the other hand, In the first line, when item is null, SetMarker method is not even called and in another word you do not try to assign anything.

Sajjad Mortazavi
  • 190
  • 2
  • 12