C# 这些 ( += , -= , *= , /= ) 运算符是什么意思?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13308399/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 08:10:39  来源:igfitidea点击:

What do these ( += , -= , *= , /= ) Operators Mean?

c#operators

提问by Ian Lundberg

I have been looking everywhere to figure out what these mean and how they are used +=, -=, *=, /=, the most I have found is that they are, "Assignment by Addition", "Assignment by Difference", "Assignment by Product", "Assignment by Quotient", etc, but I can't figure out when or how they are used. If anyone can please explain this to me I would be very grateful. thanks

我一直在到处找弄清楚这些意味着什么,他们是如何使用的+=-=*=/=,我已经找到了最重要的是,他们是“分配添加”,“差量分配”,“按产品转让”,“转让按商数”等,但我无法弄清楚它们何时或如何使用。如果有人可以向我解释这一点,我将不胜感激。谢谢

采纳答案by Totero

They are shorthand:

它们是速记:

a += b

a += b

is the same as

是相同的

a = a + b

a = a + b

Etc...

等等...

so

所以

  • a -= bis equivalent to a = a - b
  • a *= bis equivalent to a = a * b
  • a /= bis equivalent to a = a / b
  • a -= b相当于 a = a - b
  • a *= b相当于 a = a * b
  • a /= b相当于 a = a / b

As Kevin Brydon suggested - Familiarize yourself with the operators in C# here.

正如 Kevin Brydon 建议的那样 - 在此处熟悉 C# 中的运算符。

回答by Ravindra Bagale

these are shorthand operators.
these are used when you does the operation & stores result into one of the variable between them. that is you store result into one of your operand suppose example
1)x=x+y;
here you can do x+=y;
ex 2) x=x+1;
here you can do x+=1;

这些是速记运算符。
当您执行操作并将结果存储到它们之间的变量之一时会使用这些。也就是说,您将结果存储到您的操作数之一中,假设示例
1)x=x+y;
在这里你可以做 x+=y;
例 2) x=x+1;
在这里你可以做 x+=1;

回答by Michael Krelin - hacker

Roughly, var *operator*= expressionmeans var = var *operator* expression. Also, I've heard there's a documentation somewhere.

粗略地说,var *operator*= expression意味着var = var *operator* expression。另外,我听说某处有文档。

回答by andy

These are Assignment operators(Shorthands)

这些是赋值运算符(简写)

a += 1; is equal to a =  a + 1;

b -= 1; is equal to b =  b - 1;

a *= 1; is equal to a =  a * 1;

b /= 1; is equal to b =  b / 1;

Refer:Link

参考:链接

回答by wxyz

a+=1 means a = a+1
a-=2 means a = a-2
a*=3 means a = a*3
a/=4 means a = a/4

回答by Rawling

See 7.13 Assignment operatorsin the specand its subsections., specifically 7.13.2 Compound assignment:

请参阅7.13 Assignment operators规范及其小节,特别是7.13.2 Compound assignment

An operation of the form x op= y is processed by applying binary operator overload resolution (Section 7.2.4) as if the operation was written x opy. Then,

?If the return type of the selected operator is implicitly convertible to the type of x, the operation is evaluated as x = x opy, except that x is evaluated only once.

?Otherwise, if the selected operator is a predefined operator, if the return type of the selected operator is explicitly convertible to the type of x, and if y is implicitly convertible to the type of x, then the operation is evaluated as x = (T)(x opy), where T is the type of x, except that x is evaluated only once.

?Otherwise, the compound assignment is invalid, and a compile-time error occurs.

x op= y形式的运算是通过应用二元运算符重载解析(第 7.2.4 节)来处理的,就好像该运算被写成 x opy 一样。然后,

? 如果所选运算符的返回类型可隐式转换为 x 的类型,则该操作的计算结果为 x = x opy,但 x 仅计算一次。

?否则,如果所选运算符是预定义的运算符,如果所选运算符的返回类型可显式转换为 x 的类型,并且如果 y 可隐式转换为 x 的类型,则该操作的计算结果为 x = ( T)(x opy),其中 T 是 x 的类型,只是 x 只计算一次。

?否则复合赋值无效,会出现编译时错误。

回答by Mike Parkhill

It's a short form. So instead of writing:

这是一个简短的形式。所以而不是写:

x = x + 1;

x = x + 1;

You can simply write:

你可以简单地写:

x += 1;

x += 1;

It has the same affect.

它具有相同的影响。