我们为所有逻辑运算符、移位运算符、加法运算符和所有乘法运算符提供等效的赋值运算符。
为什么逻辑运算符被排除在外? 是否有充分的技术原因说明它很难?
最佳答案
Why did the logical operators get left out? Is there a good technical reason why it is hard?
他们没有。如果需要,您可以执行 &=
或 |=
或 ^=
。
bool b1 = false;
bool b2 = true;
b1 |= b2; // means b1 = b1 | b2
||
和 &&
运算符没有复合形式,因为坦率地说,它们有点傻。在什么情况下你会想说
b1 ||= b2;
b1 &&= b2;
如果左侧不变,则右侧不会被评估?似乎只有少数人真正会使用这个功能,为什么要放它呢?
有关复合运算符的更多信息,请参阅我的严肃文章:
https://learn.microsoft.com/en-us/archive/blogs/ericlippert/compound-assignment-part-one
以及此处的后续愚人节文章:
https://learn.microsoft.com/en-us/archive/blogs/ericlippert/compound-assignment-part-two
关于c# - 为什么 C# 中没有 ||= 或 &&= 运算符?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6346001/