且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

C#中的增量和后增量

更新时间:2021-11-04 06:16:07

简单的答案是:将这些增量运算符用作单个只有语句,永远不要将它们与其他语句组合!这根本不会减慢程序的速度,编译器无论如何都必须创建额外的变量。你用这种组合造成的唯一一件事是:混乱。
The simple answer is: use those increment operators as single statements only, never combine them with other statements! That won't slow down your program at all, the compiler will have to create the extra variables anyway. The only thing you cause with such combinations is: confusion.


不要这样做。

看看这个答案:表达式如何工作c = ++ a + ++ a; [ ^ ]


您处于灰色区域。在C / C ++ / C#定义中,声明允许编译器更改表达式中元素的顺序(以及更多),并且由于表达式取决于操作的顺序,因此结果是不可预测的。另一个编译器将给出不同的结果。



使用调试器并显示生成的汇编代码,以查看代码的翻译方式。
You are in gray zone. In C/C++/C# definitions, it is stated that the compiler is allowed to change the order of elements in an expression (and more), and since your expression depend on the order of operations, the result is not predictable. Another compiler will give different results.

Use a debugger and display assembly code generated to see how your code was translated.