C# 使用管道和 & 符号传递多个枚举值之间的区别

发布于 2024-10-01 07:55:15 字数 183 浏览 1 评论 0原文

C# 接受这个:

this.MyMethod(enum.Value1 | enum.Value2);

和这个:

this.MyMethod(enum.Value1 & enum.Value2);

有什么区别?

C# accepts this:

this.MyMethod(enum.Value1 | enum.Value2);

and this:

this.MyMethod(enum.Value1 & enum.Value2);

Whats the difference?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(5

醉南桥 2024-10-08 07:55:15

当您执行|时,您选择了两者。当您执行 & 时,您只会看到重叠的内容。

请注意,这些运算符仅在您将 [Flags] 属性应用于枚举时才有意义。请参阅http://msdn.microsoft.com/en-us/library/system.flagsattribute.aspx 有关此属性的完整说明。

作为示例,以下枚举:

[Flags]
public enum TestEnum
{
    Value1 = 1,
    Value2 = 2,
    Value1And2 = Value1 | Value2
}

以及一些测试用例:

var testValue = TestEnum.Value1;

这里我们测试 testValueValue1And2 重叠(即是其中的一部分):

if ((testValue & TestEnum.Value1And2) != 0)
    Console.WriteLine("testValue is part of Value1And2");

这里我们测试 是否testValue 完全等于 Value1And2。这当然不是真的:

if (testValue == TestEnum.Value1And2)
    Console.WriteLine("testValue is equal to Value1And2"); // Will not display!

这里我们测试 testValueValue2 的组合是否完全等于 Value1And2

if ((testValue | TestEnum.Value2) == TestEnum.Value1And2)
    Console.WriteLine("testValue | Value2 is equal to Value1And2");

When you do |, you select both. When you do &, you only what overlaps.

Please note that these operators only make sense when you apply the [Flags] attribute to your enum. See http://msdn.microsoft.com/en-us/library/system.flagsattribute.aspx for a complete explanation on this attribute.

As an example, the following enum:

[Flags]
public enum TestEnum
{
    Value1 = 1,
    Value2 = 2,
    Value1And2 = Value1 | Value2
}

And a few test cases:

var testValue = TestEnum.Value1;

Here we test that testValue overlaps with Value1And2 (i.e. is part of):

if ((testValue & TestEnum.Value1And2) != 0)
    Console.WriteLine("testValue is part of Value1And2");

Here we test whether testValue is exactly equal to Value1And2. This is of course not true:

if (testValue == TestEnum.Value1And2)
    Console.WriteLine("testValue is equal to Value1And2"); // Will not display!

Here we test whether the combination of testValue and Value2 is exactly equal to Value1And2:

if ((testValue | TestEnum.Value2) == TestEnum.Value1And2)
    Console.WriteLine("testValue | Value2 is equal to Value1And2");
魔法少女 2024-10-08 07:55:15
this.MyMethod(enum.Value1 | enum.Value2);

这会将两个枚举值按位“或”在一起,因此如果 enum.Value 为 1 并且 enum.Value2 为 2,则结果将是 3 的枚举值 (如果存在,否则它只是整数 3)。

this.MyMethod(enum.Value1 & enum.Value2);

这会将两个枚举值按位“与”在一起,因此如果 enum.Value 为 1 并且 enum.Value2 为 3,则结果将是 1 的枚举值。

this.MyMethod(enum.Value1 | enum.Value2);

This will bitwise 'OR' the two enum values together, so if enum.Value is 1 and enum.Value2 is 2, the result will be the enum value for 3 (if it exists, otherwise it will just be integer 3).

this.MyMethod(enum.Value1 & enum.Value2);

This will bitwise 'AND' the two enum values together, so if enum.Value is 1 and enum.Value2 is 3, the result will be the enum value for 1.

黎夕旧梦 2024-10-08 07:55:15

一种是按位或,另一种是按位与。在前一种情况下,这意味着在一个或另一个中设置的所有位都在结果中设置。在后一种情况下,这意味着在两者中设置的所有共同位都在结果中设置。您可以在 Wikipedia 上阅读有关按位运算符的内容。

示例:

enum.Value1 = 7  = 00000111
enum.Value2 = 13 = 00001101

然后

enum.Value1 | enum.Value2 = 00000111
                           |00001101
                          = 00001111
                          = 15

enum.Value1 & enum.Value2 = 00000111
                           &00001101
                          = 00000101   
                          = 5

One is bitwise-or, the other is bitwise-and. In the former case this means that all the bits that are set in one or the other are set in the result. In the latter case this means that all the bits that are in common and set in both are set in the result. You can read about bitwise operators on Wikipedia.

Example:

enum.Value1 = 7  = 00000111
enum.Value2 = 13 = 00001101

then

enum.Value1 | enum.Value2 = 00000111
                           |00001101
                          = 00001111
                          = 15

and

enum.Value1 & enum.Value2 = 00000111
                           &00001101
                          = 00000101   
                          = 5
这个俗人 2024-10-08 07:55:15

这个问题有一个很好的解释:What does the [Flags] Enum Attribute Meaning in C#?

This question has a nice explanation: What does the [Flags] Enum Attribute mean in C#?

喵星人汪星人 2024-10-08 07:55:15

枚举参数可以是二进制数,例如

enum WorldSides
{
    North=1,
    West=2,
    South=4,
    East=8
}

WorldSides.North | WorldSides.West = both values -> 3

So、|用于组合值。

使用&去除部分值,例如

if (ws & WorldSides.North)
{
     //  ws has north component
}

Enum parameters can be binary numbers, for example

enum WorldSides
{
    North=1,
    West=2,
    South=4,
    East=8
}

WorldSides.North | WorldSides.West = both values -> 3

So, | is used to combine the values.

Use & to strip some part of the value, for example

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