Null-conditional operator chaining seems very complicated - am I missing something? #4016
-
A bit of contextI learned today that the null-conditional operators (?. and ?[]) impact more than just a single member access, but in fact the entire call chain that may follow as is explained in the docs:
What confuses meSo far so good. What confuses me however is that very subtle changes to this sort of chain can have a big impact on what actually happens:
|
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 4 replies
-
You're completely understanding things. To answer your questions
Yes. Absolutely. And the behavior you see is intentional.
It is implied by the grammar rules here. Effectively you get two entirely different shaped trees here. I'm on mobile, but I can try to draw them out for you later!
A simple way to think about it is that you should use ?. If you think the thing preceding you could be null. And that parentheses snips off the tree and makes that an independent chunk. I can def provide more information if you have more questions :-). But I'm mobile, so this is all brief. |
Beta Was this translation helpful? Give feedback.
-
Yes, this was debated at some length. I think most of the conversations are buried in the archives on CodePlex since they occurred prior to the team migrating to Github. There are language design meeting notes on the subject though: |
Beta Was this translation helpful? Give feedback.
-
If var x = A?.B;
x.C; |
Beta Was this translation helpful? Give feedback.
You're completely understanding things.
To answer your questions
Yes. Absolutely. And the behavior you see is intentional.
It is implied by the grammar rules here. Effectively you get two entirely different shaped trees here. I'm on mobile, but I can try to draw them out for you later!
A simple way to think about it is t…