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
Release: 2025-02-02 14:51:09
Original
215 people have browsed it

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:

[Flags]
public enum Options
{
    None = 0,
    Option1 = 1,
    Option2 = 2,
    Option3 = 4,
    Option4 = 8
}
Copy after login

<确> Correct statement:

[Flags] <查> Check the combination of the inspection logo

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

[Flags]
public enum MyColors
{
    Yellow,  // 0
    Green,   // 1
    Red,     // 2
    Blue     // 3
}
Copy after login
<位> Use bit operator

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

[Flags]
public enum MyColors
{
    Yellow = 1,
    Green = 2,
    Red = 4,
    Blue = 8
}
Copy after login

<底> 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:
if (myProperties.AllowedColors.HasFlag(MyColor.Yellow))
{
    // 允许黄色...
}
Copy after login
00000100

Green: 00000010

AllowedColors (Red | Green):

00000110
if ((myProperties.AllowedColors & MyColor.Yellow) == MyColor.Yellow)
{
    // 允许黄色...
}
Copy after login

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 of this Website
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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template