SoFunction
Updated on 2025-03-07

Detailed explanation of the use of Flags features in C# enumeration

If multiple values ​​can be included, then enumerations can be used plus Flags.

Create a new Flags enumeration type:

[Flags]
    public enum Show
    {
        A = 0x00000001,
        B = 0x00000010,
        C = 0x00000100,
        D = 0x00001000,
    }

Merge multiple values

Merge multiple, use|

Show show =  | 

Determine whether there is a value

A simple way is to use HasFlag, but one way is to use&

 Show show= | ;
  ();
  //other  bool Include = (show & )!=0;

Remove a value

Show show= | ;
  show=show & (~);

Inverse a value

 Show show =  | ;
  bool Include = (show & )!=0;
  if(Include)
  {
     show=show & (~);
  }
  else
  {
     show=show | ;
  }

refer to:

C# bitfields[flags]

This is the end of this article about using Flags features in C# enumeration. For more information about C# enumeration Flags features, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!