&是什么QUOT;计数++ QUOT;在C#中返回?
问题描述:
恰好碰到了一些代码,这不是做什么,我想它应该。难道其他人认为这应该返回1?是否有一个很好的解释,为什么它不?
Just ran into a bit of code that wasn't doing what I thought it should. Do other people think this should return 1? Is there a good explanation as to why it doesn't??
int count = 0;
count++.ToString(); // Returns 1 no?
我一直以为计数++是一样的计算= + 1 ...
I always thought count++ was the same as count = count + 1...
答
X ++
是后的增量运营商。这意味着,值 X
递增,但的 老
的(非增量)x值返回(在你的情况下0,到的ToString
应用)
x++
is a post increment operator. It means that the value of x
is incremented, but the old (non-incremented) value of x
is returned (0 in your case, to which ToString
is applied).
要搞定的。你想要的行为,使用的前的递增运算符 ++ X
。
To get the behavior you want, use the pre increment operator ++x
.