C# X = X++ 有什么区别;VS X++;?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/226002/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What's the difference between X = X++; vs X++;?
提问by Ahmed Atia
Have you ever tried this before?
你以前试过这个吗?
static void Main(string[] args)
{
int x = 10;
x = x++;
Console.WriteLine(x);
}
Output: 10.
输出:10。
but for
但对于
static void Main(string[] args)
{
int x = 10;
x++;
Console.WriteLine(x);
}
Output: 11.
输出:11。
Could anyone explain why this?
谁能解释为什么会这样?
采纳答案by Brian R. Bondy
X++ will increment the value, but then return its old value.
X++ 将增加该值,然后返回其旧值。
So in this case:
所以在这种情况下:
static void Main(string[] args)
{
int x = 10;
x = x++;
Console.WriteLine(x);
}
You have X at 11 just for a moment, then it gets back to 10 because 10 is the return value of (x++).
你有 X 在 11 只是片刻,然后它回到 10 因为 10 是 (x++) 的返回值。
You could instead do this for the same result:
您可以改为为相同的结果执行此操作:
static int plusplus(ref int x)
{
int xOld = x;
x++;
return xOld;
}
static void Main(string[] args)
{
int x = 10;
x = plusplus(x);
Console.WriteLine(x);
}
It is also worth mentioning that you would have your expected result of 11 if you would have done:
还值得一提的是,如果您这样做,您将获得 11 的预期结果:
static void Main(string[] args)
{
int x = 10;
x = ++x;
Console.WriteLine(x);
}
回答by Konrad Rudolph
In the assignment x = x++
you firstextract the old value of x
to use in evaluating the right-hand side expression, in this case 'x'; then, you increment x
by 1. Last, you assign the results of the expression evaluation (10) to x
via the assignment statement.
在赋值中,x = x++
您首先提取x
用于评估右侧表达式的旧值,在本例中为“x”;然后,增加x
1。最后,x
通过赋值语句将表达式求值 (10) 的结果赋值给。
Perhaps an equivalent code would make the predicament clear:
也许等效的代码可以使困境变得清晰:
var tmp = x;
x++;
x = tmp;
This is the equivalent of your x = x++
code in C#.
这相当于您x = x++
在 C#中的代码。
回答by Vincent Ramdhanie
The behaviour of x++ is to increment x but return the value beforethe increment. Its called a post increment for this reason.
x++ 的行为是递增 x 但返回递增之前的值。由于这个原因,它被称为后增量。
So x = x++; simply put will
所以 x = x++; 简单地说就是
1. return the value, then
1.返回值,然后
2. increment x, then
2. 增加 x,然后
3. assign the original value(returned in step 1) of x to x.
3. 将 x 的原始值(在步骤 1 中返回)分配给 x。
回答by rp.
x = 10
x = ++x
x
would end up equalling 11.
x
最终将等于 11。
回答by matt b
This isn't answering the question directly, but why in the world would anyone use
这不是直接回答问题,但为什么世界上有人会使用
x = x++;
?
?
It totally defeats the purpose of the post-increment/pre-increment operator.
它完全违背了后增量/前增量运算符的目的。
回答by Anthony Potts
By definition, x++, returns the value of x and then increments x.
根据定义,x++ 返回 x 的值,然后递增 x。
http://blogs.msdn.com/lucabol/archive/2004/08/31/223580.aspx
http://blogs.msdn.com/lucabol/archive/2004/08/31/223580.aspx
回答by Chris Marasti-Georg
x++;
does the following:
执行以下操作:
int returnValue = x;
x = x+1;
return returnValue;
As you can see, the original value is saved, x is incremented, and then the original value is returned.
可以看到,原值被保存,x递增,然后返回原值。
What this ends up doing is saving the value 10 somewhere, setting x equal to 11, and then returning 10, which causes x to be set back to 10. Note that x does actually become 11 for a few cycles (assuming no compiler optimization).
这样做的结果是将值 10 保存在某处,将 x 设置为 11,然后返回 10,这会导致 x 重新设置为 10。请注意,在几个周期内,x 实际上变为 11(假设没有编译器优化) .
回答by sundar - Reinstate Monica
The result of the assignment
任务的结果
x = x++;
is undefined in C and C++, and I would guess the same with C# too.
在 C 和 C++ 中未定义,我猜 C# 也是如此。
So, the actual sequence of operations that occurs depends on how the compiler decides to implements it, there's no guarantee whether the assignment or the increment will occur first.
(this is well defined in C#, as Jon Skeet has pointed out in the comments. Though I now feel this answer is of much less value now, I'm keeping this post undeleted for the OP's question and its answer in the comments.)
因此,实际发生的操作顺序取决于编译器决定如何实现它,不能保证首先发生赋值还是增量。
(这在 C# 中有很好的定义,正如 Jon Skeet 在评论中指出的那样。虽然我现在觉得这个答案现在的价值要小得多,但我不会删除这篇文章,因为 OP 的问题及其在评论中的答案。)
However, in this case, it appears the sequence of operations that happens is:
但是,在这种情况下,发生的操作顺序似乎是:
- the old value (10) of x is saved
- x is incremented for the ++ part
- the old value is now assigned to x for the assignment
- 保存 x 的旧值 (10)
- x 为 ++ 部分递增
- 旧值现在分配给 x 进行赋值
In this way, though the increment occurs, it is overtaken by the assignment with old value, thus keeping x at 10.
这样,虽然发生了增量,但它被具有旧值的赋值所取代,从而使 x 保持在 10。
HTH
HTH
回答by Jarrett Meyer
As a standalone statement, x++;
is both an increment and assignment. It seems that there is some confusions as to what happens when. If we have
作为一个独立的语句,x++;
既是增量又是赋值。似乎对什么时候发生的事情有些困惑。如果我们有
int x = 10;
int y = (x++) + 2;
We will get x = 11
and y = 12
. The current value of x is assigned, and thenthe increment and reassignment of x takes place. So, when using the same variable,
我们会得到x = 11
和y = 12
。x 的当前值被赋值,然后x 的增量和重新赋值发生。所以,当使用相同的变量时,
int x = 10; // Create a variable x, and assign an initial value of 10.
x = x++; // First, assign the current value of x to x. (x = x)
// Second, increment x by one. (x++ - first part)
// Third, assign the new value of x to x. (x++ - second part)
int x = 10; // Create a variable x, and assign an initial value of 10.
x = x++; // First, assign the current value of x to x. (x = x)
// Second, increment x by one. (x++ - first part)
// Third, assign the new value of x to x. (x++ - second part)
Any way you look at it, the new value of x is 11.
不管你怎么看,x 的新值都是 11。
I was completely wrong on that one.
我完全错了。
回答by Nick
Try calling ++x and see if that works.
尝试调用 ++x 并查看是否有效。