c# - 与运算符优先级混淆

标签 c#

<分区>

它们之间的区别 Operator Priority使用前缀和后缀

    // program 01

    int x =10;
    int c = x++ + ++x;
    Console.WriteLine(c); // output will be 22

    // program 02

    int x =10;
    int c = ++x + x++;
    Console.WriteLine(c); // output will be 22

    // program 03

    int x =10;
    int c = ++x + x;
    Console.WriteLine(c); // output will be 22

最佳答案

下面是这些表达式的计算方式:

1:

x++ + ++x;
 |  |  |
 1  3  2

1: increments x to 11, returns value before increment (10)
2: increments x to 12, returns value after increment (12)
3: 10 + 12 = 22

2:

++x + x++;
 |  |  |
 1  3  2

1: increments x to 11, returns value after increment (11)
2: increments x to 12, returns value before increment (11)
3: 11 + 11 = 22

3:

++x + x
 |  | |
 1  3 2

1: increments x to 11, returns value after increment (11)
2: returns value of x (11)
3: 11 + 11 = 22

值得注意的是,某些 C/C++ 实现可能会以不同的顺序计算此表达式。此页面包含与 OP 相同的未定义行为示例。 http://en.cppreference.com/w/cpp/language/eval_order

关于c# - 与运算符优先级混淆,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40367561/

相关文章:

c# - 读取ISO 8859 1编码,用webclient下载xml流

c# - SQL 服务器 : way to see final query with filled parameters

c# - 从 View 模型保存到 ASP.NET MVC 中的模型

c# - 后期如何回滚事务?

c# - C#中如何进行四舍五入

c# - 将 RSA 公钥编码为 DER 格式

c# - ASP.NET MVC 邀请系统

c# - 如何使用 C# System.Diagnostics.Process 与提示交互?

c# - Windows Phone 8.1 中的录像机应用程序

c# - 使用连接字符串的 DocumentDB .Net 客户端