为什么 C# 中没有 ||= 或 &&= 运算符? [英] Why are there no ||= or &&= operators in C#?
问题描述
对于所有逻辑运算符、移位运算符、加法运算符和所有乘法运算符,我们都有等效的赋值运算符.
We have equivalent assignment operators for all Logical operators, Shift operators, Additive operators and all Multiplicative operators.
为什么逻辑运算符被遗漏了?有什么好的技术原因为什么很难吗?
Why did the logical operators get left out? Is there a good technical reason why it is hard?
推荐答案
为什么逻辑运算符被遗漏了?有什么好的技术原因为什么很难吗?
Why did the logical operators get left out? Is there a good technical reason why it is hard?
他们没有.如果需要,您可以执行 &=
或 |=
或 ^=
.
They didn't. You can do &=
or |=
or ^=
if you want.
bool b1 = false;
bool b2 = true;
b1 |= b2; // means b1 = b1 | b2
||
和 &&
运算符没有复合形式,因为坦率地说,它们有点傻.在什么情况下你想说
The ||
and &&
operators do not have a compound form because frankly, they're a bit silly. Under what circumstances would you want to say
b1 ||= b2;
b1 &&= b2;
这样,如果左侧不改变,则不会评估右侧?似乎只有少数人会真正使用此功能,为什么要添加它?
such that the right hand side is not evaluated if the left hand side does not change? It seems like only a few people would actually use this feature, so why put it in?
有关复合运算符的更多信息,请参阅我的严肃文章:
https://docs.microsoft.com/en-us/archive/blogs/ericlippert/compound-assignment-part-one
For more information about the compound operators, see my serious article here:
https://docs.microsoft.com/en-us/archive/blogs/ericlippert/compound-assignment-part-one
以及这里的后续愚人节文章:
https://docs.microsoft.com/en-us/archive/blogs/ericlippert/compound-assignment-part-two
and the follow-up April-Fools article here:
https://docs.microsoft.com/en-us/archive/blogs/ericlippert/compound-assignment-part-two
这篇关于为什么 C# 中没有 ||= 或 &&= 运算符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!