Is the null coalesce operator thread safe?

前端 未结 4 901
隐瞒了意图╮
隐瞒了意图╮ 2020-12-06 09:28

So this is the meat of the question: Can Foo.Bar ever return null? To clarify, can \'_bar\' be set to null after it\'s evaluated as non-null and before it\'s value is retur

4条回答
  •  天涯浪人
    2020-12-06 09:57

    The getter will never return null.

    This is because when the read is performed on the variable (_bar) the expression is evaluated and the resulting object (or null) is then "free" of the variable (_bar). It is the result of this first evaluation which is then "passed" to the coalesce operator. (See Reed's good answer for the IL.)

    However, this is not thread-safe and an assignment may easily be lost for the same reason as above.

提交回复
热议问题