What is the use of ENUM Flags

“Flags” is an ENUM attribute. If you want to set multiple values to an ENUM we need to use Flags.

 

[Flags]

enum MyColors

{

Green = 0,

Red = 1,

Blue = 2

};

 

In the below code we are setting “MyColors” ENUM to “Blue” and “Green” value.

 

MyColors color = MyColors.Blue | MyColors.Green;

if ((color & MyColors.Blue) == MyColors.Blue)

{

      Console.WriteLine(” Blue”);

}

if ((color & MyColors.Green) == MyColors.Green)

{

      Console.WriteLine(” Green”);

}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s