Home >Backend Development >C++ >How Does the C# [Flags] Enum Attribute Work with Bitwise Operations?

How Does the C# [Flags] Enum Attribute Work with Bitwise Operations?

Patricia Arquette
Patricia ArquetteOriginal
2025-02-02 14:51:09167browse

How Does the C# [Flags] Enum Attribute Work with Bitwise Operations?

In -depth understanding of [Flags] in C# enumeration attributes

In C#, the

attribute plays a vital role when defining a set of possible sets of possibilities. These enumerations are usually used together with the positioning of the position to combine and operate multiple options at the same time.

[Flags] [Flags] The role of the attribute

The attribute indicates the combination of an enumeration value, not a single value. This allows:

bit operations:

Using bit or operator (| |) can be combined to include multiple options. [Flags]

Easy to read output:
    enumerated
  • Method to generate a comma -separated activity logo list to provide user -friendly representation.
  • Example usage
  • Consider the following enumeration: ToString()
The behavior of the operation

The attribute will not automatically set the enumeration value to the power of 2. In order to ensure the compatibility of the bit operation, you should manually distribute the power of 2 to the value.

Error Declaration:

<code class="language-csharp">[Flags]
public enum Options
{
    None = 0,
    Option1 = 1,
    Option2 = 2,
    Option3 = 4,
    Option4 = 8
}</code>

Correct statement:

[Flags] Check the combination of the inspection logo

The method can be used to check whether the attribute contains specific signs:

<code class="language-csharp">[Flags]
public enum MyColors
{
    Yellow,  // 0
    Green,   // 1
    Red,     // 2
    Blue     // 3
}</code>
Use bit operator

Before .NET 4, you can use the position and the operational symbol (&) to verify the existence of the logo:

<code class="language-csharp">[Flags]
public enum MyColors
{
    Yellow = 1,
    Green = 2,
    Red = 4,
    Blue = 8
}</code>

The underlying mechanism: position indicates

The value of enumeration is represented in the form of binary. When using the power of 2, the positioning charm operates each bit:

HasFlag

Red:
<code class="language-csharp">if (myProperties.AllowedColors.HasFlag(MyColor.Yellow))
{
    // 允许黄色...
}</code>
00000100

Green: 00000010

AllowedColors (Red | Green):

00000110
<code class="language-csharp">if ((myProperties.AllowedColors & MyColor.Yellow) == MyColor.Yellow)
{
    // 允许黄色...
}</code>

None value

The

logo usually with 0
    logo is not applied to the position and operation. This is because the result will always be 0. However, you can use logic comparison (==) to determine whether there is a bit of any settings.

The above is the detailed content of How Does the C# [Flags] Enum Attribute Work with Bitwise Operations?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn