简体   繁体   English

C#中的/ =运算符有什么作用?

[英]What does the /= operator in C# do?

C#中的/ =运算符做什么,什么时候使用?

It's divide-and-assign. 它是分配和分配。 x /= n is logically equivalent to x = x / n . x /= n在逻辑上等效于x = x / n

It is similar to += , -= or *= . 它类似于+=-=*= It's a shortcut for a mathematical division operation with an assignment. 这是带有赋值的数学除法运算的快捷方式。 Instead of doing 而不是做

x = x / 10;

You can get the same result by doing 您可以通过执行以下操作获得相同的结果

x /= 10;

It assigns the result to the original variable after the operation has taken place. 操作完成后,它将结果分配给原始变量。

In most languages inspired by C, the answer is: divide and assign. 在大多数受C启发的语言中,答案是:除法和赋值。 That is: 那是:

a /= b;

is a short-hand for: 是以下方面的简写:

a = a / b;

The LHS ( a in my example) is evaluated once. LHS(在我的示例中为a )被评估一次。 This matters when the LHS is complex, such as an element from an array of structures: 当LHS很复杂时(例如来自结构数组的元素),这很重要:

x[i].pqr /= 3;

a /= 2; is the same of a = a / 2; a = a / 2; .

A division and an assignment: 部门和任务:

a /= b;

is the same as 是相同的

a = (a / b);

Its simply a combination of the two operators into one. 它只是将两个运算符组合为一个。

In the following example: 在以下示例中:

double value = 10;
value /= 2;

Value will have a final value of 5. 值的最终值为5。

The =/ operator divides the variable by the operand (in this case, 2) and stores the result back in the variable. = /运算符将变量除以操作数(在这种情况下为2),然后将结果存储回变量中。

a /= b;

is the same as 是相同的

a = a / b;

Here's the msdn article on the operator. 这是有关运营商的msdn文章

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

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