为什么C#中没有|| =或&&==运算符? [英] Why are there no ||= or &&= operators in C#?

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

问题描述

对于所有逻辑运算符,Shift运算符,加法运算符和所有乘法运算符,我们都有等效的赋值运算符.

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

这篇关于为什么C#中没有|| =或&&==运算符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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