简体   繁体   English

一元运算符的差异(+ =,= +,++ x,x ++)

[英]Difference of Unary operators ( += , =+ , ++x , x++ )

What is the difference between these unary operators in C# ? 这些一元运算符在C#中有什么区别? . Can you provide me with example? 你能举例说明吗?

Please provide the name of each. 请提供每个的名称。 :) :)

+= vs =+ + = vs = +

++x vs x++ ++ x vs x ++

This has no doubt been answered before, but anyway... 这无疑已经得到了回答,但无论如何......

They differ in how they change the value and how they return the result. 它们在如何改变价值以及如何返回结果方面存在差异。

The first two += and =+ behave in the way that the first increments a variable, the other sets a variable. 前两个+==+行为方式是第一个增加变量,另一个设置变量。 They are not related. 他们没有关系。 Observe the following code: 请注意以下代码:

// +=
x = 1;
printf( x += 1 ); // outputs 2, the same as x = x+1
printf( x );      // outputs 2

// =+
x = 1;
printf( x =+ 1 ); // outputs 1, the same as x = 1;
printf( x );      // outputs 1

The next two, ++x and x++ , differ in the order their function. 接下来的两个, ++xx++ ,它们的功能顺序不同。 ++x will increment your variable by 1 and return the result. ++x将您的变量递增1并返回结果。 x++ will return the result and increment by 1. x++将返回结果并递增1。

// ++x
x = 1;
printf( ++x ); // outputs 2, the same as x = x+1
printf( x );   // outputs 2

// x++
x = 1;
printf( x++ ); // outputs 1
printf( x );   // outputs 2

They are mostly useful for for loops and while loops. 它们主要用于for循环和while循环。

In terms of speed, ++x is considered a lot faster than x++ since x++ needs to create an internal temporary variable to store the value, increment the main variable, but return the temporary variable, basically more operations are used. 就速度而言, ++x被认为比x++快很多,因为x++需要创建一个内部临时变量来存储值,增加主变量,但返回临时变量,基本上使用更多的操作。 I learned this a looong time ago, I don't know if it still applies 我在很久以前就学到了这一点,我不知道它是否仍然适用

Let's visualize the first ones, += and =+. 让我们想象出第一个,+ =和= +。

Because "+" is action, "=" is assignment, so 因为“+”是动作,所以“=”是赋值

+= is to add BEFORE assignment + =是在赋值之前添加

=+ is a bit confusing with "+", it could be "-", for example a=+7 or a=-7, anyway, it's a direct assignment. = +与“+”有点混淆,它可能是“ - ”,例如a = + 7或a = -7,无论如何,它是直接赋值。

Similarly, 同样的,

++x is "increment then return" ++ x是“递增然后返回”

x++ is "return then increase" x ++是“返回然后增加”

++x vs x++ are unary operators. ++ x vs x ++是一元运算符。 ++x means pre increment and x++ means post increment. ++ x表示预增量,x ++表示后增量。

int temp;
temp = 1;
Console.WriteLine(++temp); // Outputs 2
temp = 1;
Console.WriteLine(temp++); // outputs 1
Console.WriteLine(temp); // outputs 2

Prefix increment means: 前缀增量意味着:

The result of the operation is the value of the operand after it has been incremented. 操作的结果是操作数增加后的值。

Postfix increment means: 后缀增量意味着:

The result of the operation is the value of the operand before it has been incremented. 操作的结果是操作数增加之前的值。

Now the following: += means temp += 10; 现在如下:+ =表示临时+ = 10; // same as temp = temp + 10; //与temp = temp + 10相同;

This =+ isn't a valid operator. 这个= +不是有效的运算符。 If one does this: 如果这样做:

str = + str;  // will throw an error.
int a;
a = +2; // sort of meaningless . 2 and +2 means same.

More here: Is there such thing as a "=+" operator? 更多信息: 是否存在“= +”运算符?

https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/operators/increment-operator https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/operators/increment-operator

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM