C# 中 &= 运算符的作用是什么?

发布于 2024-10-26 01:16:37 字数 180 浏览 7 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

锦欢 2024-11-02 01:16:37

这意味着它是一个复合赋值运算符。就像:

i += 1;

就像

i = i + 1;

所以

approved &= cra.Approved;

就像

approved = approved & cra.Approved;

在这种情况下 & 是逻辑 AND 运算符(因为我们正在处理 bool 值;对于整数,它将是按位与运算符)。

有关复合赋值运算符的具体性质的更多详细信息,请参阅 C# 4 规范的第 7.17.2 节。

It means it's a compound assignment operator. Just like:

i += 1;

is like

i = i + 1;

So

approved &= cra.Approved;

is like

approved = approved & cra.Approved;

where & is the logical AND operator in this case (because we're dealing with bool 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.

几度春秋 2024-11-02 01:16:37

对于 bool 类型,它是逻辑与赋值运算符。对于整型,它是按位与赋值运算符。

它实际上等同于:

approved = approved & cra.Approved;

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:

approved = approved & cra.Approved;
雾里花 2024-11-02 01:16:37

这是一个逻辑“与”。

来自 MSDN(& Operator):

对于布尔操作数,&计算其操作数的逻辑与;

在您的代码中,这与以下内容相同:

approved = approved & cra.Approved;

It is a logical AND.

From MSDN (& Operator):

For bool operands, & computes the logical AND of its operands;

In your code this is the same as:

approved = approved & cra.Approved;
岁月染过的梦 2024-11-02 01:16:37

这是AND 赋值运算符

它基本上是在做:

approved = approved & cra.Approved;

如果之前是 true 并且 cra.Approved 是 true,则将其设置为 true。

This is the AND assignment operator.

It is basically doing:

approved = approved & cra.Approved;

Which will set approved to true if it was true previously AND cra.Approved is true.

作死小能手 2024-11-02 01:16:37

a &= b 等于 a = (a & b)

& 运算符执行按位 AND。此按位 AND 运算仅涉及设置结果中最初在 ab 中设置的那些位。

a &= b is equal to a = (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 both a and b.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文