如何用一些运算符解释空合并表达式优先级计算?

问题描述 投票:0回答:1

以下代码运行良好。在空合并

+
之后评估加法
??
背后的逻辑是什么?这怎么可能?文档在哪里解释?

    int? tNullable = 2;
    public int TestNullCoalescing()
    {
        int t = 7;
        t = tNullable + 1 ?? t;
        Debug.Assert(t == 3);
        tNullable = null;
        t= 7;
        t = tNullable + 1 ?? t;
        Debug.Assert(t == 7);

参考资料:

  1. ??和 ??= 运算符 - 空合并运算符
  2. 12.15 空合并运算符
c# nullable operator-precedence evaluation null-coalescing-operator
1个回答
0
投票

因为

null + 1
仍然是
null
(甚至没有计算
+ 1
),
??
运算符直接返回
7

© www.soinside.com 2019 - 2024. All rights reserved.