为什么没有|| =或放大器;&安培; =运营商? [英] Why are there no ||= or &&= operators?

查看:104
本文介绍了为什么没有|| =或放大器;&安培; =运营商?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们有相当的赋值运算符的所有逻辑运算符,移位运算,加法运算符和所有乘法运算。

为什么逻辑运算符被甩了呢?
是否有一个良好的技术原因,这是很难?


解决方案

  

为什么逻辑运算符被甩了呢?是否有一个良好的技术原因,这是很难?


他们没有。你可以做&安培; = | = ^ = 如果你想。

  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 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?

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆