Is the null coalesce operator thread safe?

前端 未结 4 892
隐瞒了意图╮
隐瞒了意图╮ 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:54

    Reflector says no:

    List l = null;
    var x = l ?? new List();
    

    Compiles to:

    [STAThread]
    public static void Main(string[] args)
    {
        List list = null;
        if (list == null)
        {
            new List();
        }
    }
    

    Which does not appear to be thread safe in the respect you've mentioned.

提交回复
热议问题