前& C# 中的后置增量

发布于 2024-12-21 23:09:57 字数 450 浏览 1 评论 0原文

我对 C# 编译器如何处理前后递增和递减有点困惑。

当我编写以下代码时:

int x = 4;
x = x++ + ++x;

x 之后的值为 10。我认为这是因为预增量将 x 设置为 5,这使得 5+5 的计算结果为 10代码>.那么后置增量会将x更新为6,但是这个值不会被使用,因为10将被分配给x

但是当我编码时:

int x = 4;
x = x-- - --x;

那么 x 之后将是 2 。谁能解释为什么会这样?

I am a little confused about how the C# compiler handles pre- and post increments and decrements.

When I code the following:

int x = 4;
x = x++ + ++x;

x will have the value 10 afterwards. I think this is because the pre-increment sets x to 5, which makes it 5+5 which evaluates to 10. Then the post-increment will update x to 6, but this value will not be used because then 10 will be assigned to x.

But when I code:

int x = 4;
x = x-- - --x;

then x will be 2 afterwards. Can anyone explain why this is the case?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

与往事干杯 2024-12-28 23:09:57

x-- 将是 4,但在 --x 时刻将是 3,所以它最终将是 2,那么你将得到

x = 4 - 2

顺便说一句,你的第一个案例将是 x = 4 + 6

这是一个小示例,它将打印出每个部分的值,也许这样您会更好地理解它:

static void Main(string[] args)
{
    int x = 4;
    Console.WriteLine("x++: {0}", x++); //after this statement x = 5
    Console.WriteLine("++x: {0}", ++x); 

    int y = 4;
    Console.WriteLine("y--: {0}", y--); //after this statement y = 3
    Console.WriteLine("--y: {0}", --y);

    Console.ReadKey();
}

这会打印出来

x++: 4
++x: 6
y--: 4
--y: 2

x-- will be 4, but will be 3 at the moment of --x, so it will end being 2, then you'll have

x = 4 - 2

btw, your first case will be x = 4 + 6

Here is a small example that will print out the values for each part, maybe this way you'll understand it better:

static void Main(string[] args)
{
    int x = 4;
    Console.WriteLine("x++: {0}", x++); //after this statement x = 5
    Console.WriteLine("++x: {0}", ++x); 

    int y = 4;
    Console.WriteLine("y--: {0}", y--); //after this statement y = 3
    Console.WriteLine("--y: {0}", --y);

    Console.ReadKey();
}

this prints out

x++: 4
++x: 6
y--: 4
--y: 2
蝶舞 2024-12-28 23:09:57

让我们看一下从该语句生成的 IL

IL_0002:  ldloc.0     

将 x 的值加载到堆栈中。堆栈=> (4)

IL_0003:  dup         

复制堆栈最顶层的项目。堆栈=> (4, 4)

IL_0004:  ldc.i4.1    

将 1 压入堆栈。堆栈=> (1, 4, 4)

IL_0005:  sub         

将顶部的两个值相减并将结果压入堆栈。堆栈=> (3, 4)

IL_0006:  stloc.0     

将栈顶的值存回到x。堆栈=> (4)

IL_0007:  ldloc.0     

将x的值加载回堆栈。堆栈=> (3, 4)

IL_0008:  ldc.i4.1    

将值 1 加载到堆栈中。堆栈=> (1,3,4)

IL_0009:  sub         

将两者相减。堆栈=> (2, 4)

IL_000A:  dup         

复制顶部值 => (2, 2, 4)

IL_000B:  stloc.0     

将最高值存储回x。堆栈=> (2, 4)

IL_000C:  sub      

减去两个最高值。堆栈=> (2)

IL_000D:  stloc.0  

将此值存回到x 中。 x == 2

Lets have a look at the IL that gets generated from that statement

IL_0002:  ldloc.0     

Loads the value of x onto the stack. Stack => (4)

IL_0003:  dup         

Duplicates the topmost item on the stack. Stack => (4, 4)

IL_0004:  ldc.i4.1    

Push 1 onto the stack. Stack => (1, 4, 4)

IL_0005:  sub         

Subtract the two top values and push result onto the stack. Stack => (3, 4)

IL_0006:  stloc.0     

Store the topmost value of the stack back to x. Stack => (4)

IL_0007:  ldloc.0     

Load the value of x back into the stack. Stack => (3, 4)

IL_0008:  ldc.i4.1    

Load the value 1 onto the stack. Stack => (1, 3, 4)

IL_0009:  sub         

Subtract the two. Stack => (2, 4)

IL_000A:  dup         

Duplicate the top value => (2, 2, 4)

IL_000B:  stloc.0     

Store the top value back to x. Stack => (2, 4)

IL_000C:  sub      

Subtract the two top values. Stack => (2)

IL_000D:  stloc.0  

Store this value back into x. x == 2

回眸一遍 2024-12-28 23:09:57

从你的评论来看:

我认为后增量和前增量是在评估完整代码行之后/之前执行的 - 但它们是在表达式中每个项目的评估之后/之前执行的。

你的误解是一种极其常见的误解。请注意,在某些语言(例如 C)中,未指定副作用何时可见,因此在 C 中您的语句为 true 是合法的,但不是必需的。

在 C# 中情况并非如此;在 C# 中,表达式左侧代码的副作用总是在右侧代码执行之前发生(来自单个线程;在多线程场景中,所有的赌注都被取消了。

)关于 C# 中增量运算符的详细解释,请参阅:

i++ 和 ++i 有什么区别?< /a>

这里有很多额外的链接,指向我写的关于这个经常被误解的主题的文章。

From your comment:

I thought that post- and pre-increments are executed after / before evaluation of the complete codeline - but they are executed after / before the evaluation of each item in the expression.

Your misunderstanding is an extremely common one. Note that in some languages, like C, it is not specified when the side effect becomes visible and it is therefore legal, but not required, for your statement to be true in C.

This is not the case in C#; in C# side effects of code on the left side of an expression are always observed to happen before code on the right side executes (from a single thread; in multithreaded scenarios all bets are off.)

For a more detailed explanation of what the increment operators do in C#, see:

What is the difference between i++ and ++i?

There are a great many additional links there to articles I've written on this often-misunderstood topic.

困倦 2024-12-28 23:09:57

最有趣的是,使用 C++.Net 编译器你会得到完全不同的答案。

int x = 4;
x = x++ + ++x; // x = 11
x = 4;
x = x-- - --x; // x = -1

当然,结果的差异是由不同的语义决定的 - 这似乎很正常。但是,尽管我知道两个 .net 编译器对于这些基本事情的行为方式并不相似,但我也很困惑。

The most interesting thing that you'll get a completely different answer with C++.Net compiler.

int x = 4;
x = x++ + ++x; // x = 11
x = 4;
x = x-- - --x; // x = -1

Of course the difference in results is determined by different semantics - it seems normal. But despite the understanding the fact that two .net compilers don't behave in a similar manner for such basic things confuses me too.

半透明的墙 2024-12-28 23:09:57

在这个例子中,

int x = 4;
x = x++ + ++x;

你可以像这样打破它:

x = 4++; which is = 5
x = 4 + ++5; which is 4 + 6
x = 10

类似地,

int x = 4;
x = x-- - --x;

这里,

x = 4--; which is = 3
x = 4 - --3; which is 4 - 2
x = 2

简单地说,你可以说,替换 x 的当前值,但对于每个 ++ 或 -- 从 x 中添加/减去一个值。

In this example,

int x = 4;
x = x++ + ++x;

you can break it like:

x = 4++; which is = 5
x = 4 + ++5; which is 4 + 6
x = 10

Similarly,

int x = 4;
x = x-- - --x;

Here,

x = 4--; which is = 3
x = 4 - --3; which is 4 - 2
x = 2

Simply putting you can say, replace the current value of x, but for every ++ or -- add/subtract a value from x.

谈下烟灰 2024-12-28 23:09:57

我认为对 ++ + ++ 情况的解释是错误的:

command...........value of x

........................undefined

int x=4 .......4

x++.........................5 (第一个被加数为 4)

++x......................... .....6 (第二个被加数为 6)

x=summand1+summand2 ..4+6=10

类似 -- - -- 情况的解释是

命令............的值x

......................未定义的

整数 x=4 ........................4

x--........................3(减数为 4)

--x.........................2(减数为 2)

x=减法-减数 ..4-2=10

I think the explanation for the ++ + ++ case is wrong:

command...........value of x

..................undefined

int x=4 ..........4

x++...............5 (first summand is 4)

++x...............6 (second summand is 6)

x=summand1+summand2 ..4+6=10

Analogous the explanation for the -- - -- case is

command...........value of x

..................undefined

int x=4 ..........4

x--...............3 (subtactor is 4)

--x...............2 (subtrahend is 2)

x=subtractor-subtrahend ..4-2=10

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文