What's happening with this expression? b = a + (a = a + 5)

前端 未结 4 1216
面向向阳花
面向向阳花 2021-01-30 15:47
a = 5
b = a + (a = a + 5)

result b = 15

Why the first \'a\' do not changes after that (a = a + 5)? But why second one changes? What exactly is

4条回答
  •  没有蜡笔的小新
    2021-01-30 16:27

    Unlike C and C++, the order of evaluation of subexpressions are left to right in C#. That's why the expression

    j= ++i + ++i ;   
    

    has well defined behavior in C# while undefined in C and C++.

    In expression

    b = a + (a = a + 5)  
    

    left a will be evaluated first, then a+5 is evaluated and assigned to a and after addition of both evaluated subexpression, b will have value 15.

提交回复
热议问题