有人可以解释一下吗?操作员?
可能的重复:
C#:i++ 和 ++i 之间有什么区别?< /a>
我经常看到这个运算符 (++)。我知道它最终会做什么,但似乎有一些我不明白的规则。例如,将它放在要使用它的变量之前或之后似乎很重要。有人可以解释一下吗?
Possible Duplicate:
C#: what is the difference between i++ and ++i?
I see this operator (++) very often. I know what it does ultimately, but it seems like there's some rules I don't understand. For example, it seems to matter if you put it before or after the variable you're using it on. Can someone explain this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
如果将 ++ 运算符放在变量之前,则变量首先递增。如果将 ++ 运算符放在变量后面,则变量之后会递增。
例如(C#):
int x = 0;
Console.WriteLine("x 是 {0}", x++); // 打印 x is 0
int y = 0;
Console.WriteLine("y 是 {0}", ++y); // 打印 y 为 1
If you put the ++ operator before the variable, it is incremented first. If you put the ++ operator after the variable, it is incremented after.
For example(C#):
int x = 0;
Console.WriteLine("x is {0}", x++); // Prints x is 0
int y = 0;
Console.WriteLine("y is {0}", ++y); // Prints y is 1
另一种看待它的方式...这里有两个函数,其作用与 prefix/postfix ++ 相同。这说明前缀在理论上更快,因为不需要临时变量来存储“前一个”值:
Another way to see it... here are two functions that do the same as prefix/postfix ++. This illustrates that prefix is, in theory, faster, because no temporary variable is needed to store the "previous" value:
好吧,如果你这样说它
首先使用变量并递增它(+1)
另一方面,如果您
首先递增变量然后使用它
well if you put it like
It first uses the variable and the increments it (+1)
On the otherhand if you
It first increments the variable and then uses it
如果将
++
运算符放在变量之前,则变量首先递增。如果将
++
运算符放在变量后面,则变量会在后面递增。例如(C#):
希望这能解决问题。
If you put the
++
operator before the variable, it is incremented first.If you put the
++
operator after the variable, it is incremented after.For example(C#):
Hope this cleared it up.
http://msdn.microsoft.com/en -us/library/36x43w8w(v=VS.80).aspx
增量运算符 (++) 将其操作数增加 1。增量运算符可以出现在其操作数之前或之后:
第一种形式是前缀增量手术。运算结果是操作数递增后的值。
第二种形式是后缀增量运算。运算的结果是操作数递增之前的值。
http://msdn.microsoft.com/en-us/library/36x43w8w(v=VS.80).aspx
The increment operator (++) increments its operand by 1. The increment operator can appear before or after its operand:
The first form is a prefix increment operation. The result of the operation is the value of the operand after it has been incremented.
The second form is a postfix increment operation. The result of the operation is the value of the operand before it has been incremented.
该语句
与完全相同,
除了,
x
仅计算一次(如果它是涉及属性获取器的表达式,则情况会有所不同)。以下两者之间的区别
在于:在第一个中,方法
DoSomething
将看到x
的前一个值之前< /em> 它被增加了。在第二个中,它将看到新(增量)值。有关详细信息,请参阅 C# 运算符在 MSDN 上。
可以为您自己的类声明自定义
++
运算符,在这种情况下,该运算符可以执行不同的操作。如果要定义自己的++
运算符,请参阅 运算符重载教程。The statement
is exactly equivalent to
except that
x
is evaluated only once (which makes a difference if it is an expression involving property getters).The difference between the following two:
Is that in the first one, the method
DoSomething
will see the previous value ofx
before it was incremented. In the second one, it will see the new (incremented) value.For more information, see C# Operators on MSDN.
It is possible to declare a custom
++
operator for your own classes, in which case the operator can do something different. If you want to define your own++
operator, see Operator Overloading Tutorial on MSDN.