C#中的逻辑运算符 [英] logical Operators in C#
本文介绍了C#中的逻辑运算符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
为什么在C#中返回值为14
int x = 2 ;
int y = x + 4 * ++ x;
MessageBox.Show(x.ToString()+ " + y.ToString());
解决方案
14是正确的
对于左侧,x = 2 + 4 *(x = 3增量) 2+ 4 * 3-> 14
Mehdi和Abhinav是正确的.
Java中的结果也是14.
等同于y = x + 4 *(x + = 1);
表达式从左到右求值.
如果将左边的x移到右边,您将得到15,y = 4 * ++ x + x;
int y = x + 4 * ++ x;
可以简化为y = x + 4 *(x + 1).
++ x是前缀一元运算符.因此,首先添加1,然后将其用于进一步的计算.
结果,表达式变成2 + 4 *(2 +1),因此变成14.
hi
The under code should return 15 but in C# return value is 14 !!!
why in C# return value is 14
int x = 2;
int y =x+ 4* ++x;
MessageBox.Show(x.ToString()+"\n"+y.ToString());
解决方案
14 is correct
For the left x=2 + 4 * (increment x =3) -> 2+ 4*3 -> 14
Mehdi and Abhinav are correct.
The result is also 14 in Java.
It is identical to y = x + 4 * (x+=1);
The expressions are evaluated left to right.
You will get 15 if the left x is moved to the right, y = 4 * ++x + x;
int y =x+ 4* ++x;
This can be simplified to y = x + 4 * (x+1).
++x is a prefix unary operator. So the 1 is first added and then it used for further calculation.
As a result expression becomes 2 + 4 * (2 + 1) and hence 14.
这篇关于C#中的逻辑运算符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文