为什么没有|| =或放大器;&安培; =运营商? [英] Why are there no ||= or &&= operators?
问题描述
我们有相当的赋值运算符的所有逻辑运算符,移位运算,加法运算符和所有乘法运算。
为什么逻辑运算符被甩了呢?
是否有一个良好的技术原因,这是很难?
为什么逻辑运算符被甩了呢?是否有一个良好的技术原因,这是很难?
块引用>他们没有。你可以做
&安培; =
或| =
或^ =
如果你想。B1布尔= FALSE;
布尔B2 = TRUE;
B1 | = B2; //意味着B1 = B1 | B2的
||
和&放大器;&安培;
运营商没有复合形式,因为坦率地说,他们是有点傻。在什么情况下你想说B1 || = B2;
B1和放大器;&安培; = B2;,使得如果左手侧不改变它的右手侧不评估?这似乎是只有少数人实际上使用该功能,那么,为什么把它?
有关复合经营者在这里看到我的认真文章:
<一个href=\"http://blogs.msdn.com/b/ericlippert/archive/2011/03/29/compound-assignment-part-one.aspx\">http://blogs.msdn.com/b/ericlippert/archive/2011/03/29/compound-assignment-part-one.aspx
和后续四月愚人文章在这里:
<一个href=\"http://blogs.msdn.com/b/ericlippert/archive/2011/04/01/compound-assignment-part-two.aspx\">http://blogs.msdn.com/b/ericlippert/archive/2011/04/01/compound-assignment-part-two.aspx
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 sayb1 ||= 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?
For more information about the compound operators see my serious article here:
http://blogs.msdn.com/b/ericlippert/archive/2011/03/29/compound-assignment-part-one.aspx
and the follow-up April-Fools article here:
http://blogs.msdn.com/b/ericlippert/archive/2011/04/01/compound-assignment-part-two.aspx
这篇关于为什么没有|| =或放大器;&安培; =运营商?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!