Using Bit flags when declaring the enum : enum base type « Data Type « C# / CSharp Tutorial






using System;

[Flags]
enum Color : uint
{
   Red = 0x01, // Bit 0
   Blue = 0x02, // Bit 1
   Yellow = 0x04, // Bit 2
   Green = 0x08 // Bit 3
}

class MainClass
{
   static void Main()
   {
      Color ops = Color.Red | Color.Yellow | Color.Green;
      
      bool UseRed = false, UseBlue   = false, UseYellow  = false, UseGreen = false;

      UseRed = (ops & Color.Red) == Color.Red;
      UseBlue    = (ops & Color.Blue) == Color.Blue;
      UseYellow  = (ops & Color.Yellow) == Color.Yellow;
      UseGreen  = (ops & Color.Green) == Color.Green;

      Console.WriteLine("Option settings:");
      Console.WriteLine("   Use Red    - {0}", UseRed);
      Console.WriteLine("   Use Blue   - {0}", UseBlue);
      Console.WriteLine("   Use Yellow - {0}", UseYellow);
      Console.WriteLine("   Use Green  - {0}", UseGreen);
   }
}
Option settings:
   Use Red    - True
   Use Blue   - False
   Use Yellow - True
   Use Green  - True








2.37.enum base type
2.37.1.Convert enum to int
2.37.2.Assign int value to all elements in an enum
2.37.3.Assign int value to the first element in an enum
2.37.4.Define enumeration base types
2.37.5.Bit Flag Enums
2.37.6.Using Bit flags when declaring the enum
2.37.7.Print out string version of an enum value
2.37.8.Using a base type of long when defining an enum
2.37.9.Using Operators with Enumerations
2.37.10.enum based on byte
2.37.11.enum based on byte, and convert enum variable back to byte