Home >Backend Development >C++ >How Does the C# [Flags] Enum Attribute Enhance Bitwise Operations and Enumeration Readability?

How Does the C# [Flags] Enum Attribute Enhance Bitwise Operations and Enumeration Readability?

Barbara Streisand
Barbara StreisandOriginal
2025-02-02 14:56:10429browse

How Does the C# [Flags] Enum Attribute Enhance Bitwise Operations and Enumeration Readability?

[Flags] enumerated attributes in [ C#: Understand its role

When defining enumeration in C#, if enumeration represents the set of values ​​rather than a single value, you can use the [Flags] property. This attribute is particularly useful in the enumeration of the use operator.

[Flags] The importance of the attribute

In contrast to the universal misunderstanding, the [Flags] attribute provides a more readable representation form through the method. The enumeration marked with [Flags] will output its value, and the value is displayed in the form of a sign of a comma separation. As shown below:

.ToString() The power value of 2

<code class="language-csharp">enum Suits { Spades = 1, Clubs = 2, Diamonds = 4, Hearts = 8 }
[Flags] enum SuitsFlags { Spades = 1, Clubs = 2, Diamonds = 4, Hearts = 8 }

...

var str1 = (Suits.Spades | Suits.Diamonds).ToString(); // "5"
var str2 = (SuitsFlags.Spades | SuitsFlags.Diamonds).ToString(); // "Spades, Diamonds"</code>

It must be emphasized that the [Flags] attribute does not automatically assign the power value of 2 to enumeration. If the value of the value is skipped, the holding of the operation in the position is incorrect, because the default value increases from 0. The correct statement:

Get different values:

To retrieve the different values ​​in the enumeration attributes, you can use multiple methods:
<code class="language-csharp">[Flags]
public enum MyColors
{
    Yellow = 1,
    Green = 2,
    Red = 4,
    Blue = 8
}</code>

Use <.> (. Net 4 and higher versions available):

  • Use the position operator (before .NET 4):

    .HasFlag()

    <code class="language-csharp">  if (myProperties.AllowedColors.HasFlag(MyColor.Yellow))
      {
          // Yellow is allowed...
      }</code>
  • The underlying mechanism
  • [Flags] Properties are allowed to operate binary values ​​by operating by bit computing. By using the power value of 2 as an enumeration value, the underlying binary of the value means that it allows efficient and meaningful calculations.
    <code class="language-csharp">  if ((myProperties.AllowedColors & MyColor.Yellow) == MyColor.Yellow)
      {
          // Yellow is allowed...
      }</code>
  • For example, if the attribute AllowEdColors use binary according to the bit or | operational symbols to be Red, Green, and Blue, then AllowedColors will save binary 00001110.

None = 0 value

In some enumerations, it contains 0 values ​​(usually named "None"). However, it should be noted that the position and & operations involved in the NONE value always return 0, so it is not suitable for testing specific signs. You can use logical comparison.

For more information and resources about the [Flags] attributes, its usage and design mode, see the MSDN documentation.

The above is the detailed content of How Does the C# [Flags] Enum Attribute Enhance Bitwise Operations and Enumeration Readability?. 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