C# 中 &= 运算符的作用是什么?
C# 中 &= 运算符的作用是什么?
例如:
bool approved;
// Approved is a property of cra, also bool
approved &= cra.Approved;
非常感谢!
What does the &= operator do in C#?
For example:
bool approved;
// Approved is a property of cra, also bool
approved &= cra.Approved;
Thanks a lot!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
这意味着它是一个复合赋值运算符。就像:
就像
所以
就像
在这种情况下
&
是逻辑 AND 运算符(因为我们正在处理bool
值;对于整数,它将是按位与运算符)。有关复合赋值运算符的具体性质的更多详细信息,请参阅 C# 4 规范的第 7.17.2 节。
It means it's a compound assignment operator. Just like:
is like
So
is like
where
&
is the logical AND operator in this case (because we're dealing withbool
values; for integers it would be the bitwise AND operator).See section 7.17.2 of the C# 4 spec for more details of the exact nature of compound assignment operators.
对于 bool 类型,它是逻辑与赋值运算符。对于整型,它是按位与赋值运算符。
它实际上等同于:
For the bool type, it is the logical-and assignment operator. For integral types, it is the bitwise-and assignment operator.
It is effectively the same as:
这是一个逻辑“与”。
来自 MSDN(& Operator):
在您的代码中,这与以下内容相同:
It is a logical AND.
From MSDN (& Operator):
In your code this is the same as:
这是AND 赋值运算符。
它基本上是在做:
如果之前是 true 并且 cra.Approved 是 true,则将其设置为 true。
This is the AND assignment operator.
It is basically doing:
Which will set approved to true if it was true previously AND cra.Approved is true.
a &= b
等于a = (a & b)
。&
运算符执行按位 AND。此按位 AND 运算仅涉及设置结果中最初在a
和b
中设置的那些位。a &= b
is equal toa = (a & b)
.The
&
operator performs a bitwise AND. This bitwise AND operation involves setting only those bits in the result that were originally set in botha
andb
.