Curious null-coalescing operator custom implicit conversion behaviour

前端 未结 5 1343
粉色の甜心
粉色の甜心 2020-11-30 16:32

Note: this appears to have been fixed in Roslyn

This question arose when writing my answer to this one, which talks about the associativity of the n

5条回答
  •  佛祖请我去吃肉
    2020-11-30 16:48

    I am not a C# expert at all as you can see from my question history, but, I tried this out and I think it is a bug.... but as a newbie, I have to say that I do not understand everything going on here so I will delete my answer if I am way off.

    I have come to this bug conclusion by making a different version of your program which deals with the same scenario, but much less complicated.

    I am using three null integer properties with backing stores. I set each to 4 and then run int? something2 = (A ?? B) ?? C;

    (Full code here)

    This just reads the A and nothing else.

    This statement to me looks like to me it should:

    1. Start in the brackets, look at A, return A and finish if A is not null.
    2. If A was null, evaluate B, finish if B is not null
    3. If A and B were null, evaluate C.

    So, as A is not null, it only looks at A and finishes.

    In your example, putting a breakpoint at the First Case shows that x, y and z are all not null and therefore, I would expect them to be treated the same as my less complex example.... but I fear I am too much of a C# newbie and have missed the point of this question completely!

提交回复
热议问题