What is the operator precedence of C# null-coalescing (??) operator?

前端 未结 4 1069
难免孤独
难免孤独 2020-12-05 18:54

I\'ve just tried the following, the idea being to concatenate the two strings, substituting an empty string for nulls.

string a=\"Hello\";
string b=\" World\         


        
4条回答
  •  抹茶落季
    2020-12-05 19:19

    It interesting that http://msdn.microsoft.com/en-us/library/6a71f45d.aspx and http://en.csharp-online.net/ECMA-334:_14.2.1_Operator_precedence_and_associativity give different precedence to ??.

    msdn:

    1. Conditional
    2. Assignment
    3. Null-coalescing
    4. Lambda

    ECMA:

    1. Null Coalescing
    2. Conditional
    3. Assignment

    I think the msdn must be wrong, consider:

    string a = null;
    string b = a ?? "foo";
    // What is b now?
    

提交回复
热议问题