- Category: .NET

In this article I will explain what and where is the use of flags attribute in enums. By setting the flags attribute over the enum, each enum value will be treated as bitflag pattern. Enum should have flags attribute present only if each value defined in the enumeration is a power of two, or a combination of defined values. Enum should have flags attribute if a bitwise operation (AND, OR, EXCLUSIVE OR) is to be performed over the values.

**How to define**

We need to define enumeration constants in the powers of two, that is, 1, 2, 4, 8, and so on. By defining in this manner individual flags in combined enumeration constants do not overlap.

Explanation with sample code

In this sample we are going to take enum, a sample class(POCO) and a main class to test it. Let's start with enum first:**Enum**

using System; namespace ConsoleApplication1 { [Flags] public enum Shapes { Circle = 1, Square = 2, Triangle = 4, Oval = 8, } }

Above we have taken *Shapes *enum and defined 4 enumerations constants in it Circle, Square, Triangle and Oval. Enumeration constants are defined in the powers of two, that is, 1, 2, 4, 8. Next move to a sample class. **Sample class (POCO**

namespace ConsoleApplication1 { public class Sample { public int Foo; public Shapes AllowedShapes; } }

Above we have taken a sample class using that *Shapes* enum as one of it's properties. This class's instance will be used ahead in our sample.**Program.cs (Main class to test)**

using System; namespace ConsoleApplication1 { class Program { static void Main(string[] args) { Sample sample = new Sample(); sample.Foo = 1; sample.AllowedShapes = Shapes.Circle | Shapes.Square; //Let's test it if((sample.AllowedShapes & Shapes.Circle) == Shapes.Circle) { Console.WriteLine("We have circle shape with us"); } if ((sample.AllowedShapes & Shapes.Square) == Shapes.Square) { Console.WriteLine("We have square shape with us"); } if ((sample.AllowedShapes & Shapes.Oval) == Shapes.Oval) { Console.WriteLine("We have oval shape with us"); } else { Console.WriteLine("We don't have oval shape with us"); } if ((sample.AllowedShapes & Shapes.Triangle) == Shapes.Triangle) { Console.WriteLine("We have triangle shape with us"); } else { Console.WriteLine("We don't have triangle shape with us"); } Console.ReadLine(); } } }

Above we have created the instance of *Sample* class and assigned its properties with appropriate values. Next we performed some and and or'd operations on it's *AllowedShapes *property and print the result accordingly. Below is the output we got after running the above sample.**Output**

**Explanation of tests we did above**

Now let's uncover how did this output comes. Behind the scenes your enum will be considered as this:

**Circle= 00000001 ****Square= 00000010****Triangle= 00000100****Oval= 00001000**

i.e in their binary representations. Now when we did *sample.AllowedShapes = Shapes.Circle | Shapes.Square* . The following caclulation took place:

** 00000001****Or'd 00000010**

-------------------------

00000011

ie. now the sample.AllowedShapes contains 00000011 value in it. Next move to the operations which we did on this property .

**1st test**:

** 00000011****And 00000001**

-------------------------

00000001 (Circle)

i.e the and operation result comes out to be 00000001 which is the value of Circle shape which means we have that shape.

**2nd test**:

** 00000011****And 00000010**

-------------------------

00000010 (Square)

i.e the and operation result comes out to be 00000010 which is the value of Square shape which means we have that shape.

**3rd test**:

** 00000011****And 00000100**

-------------------------

00000000

i.e the and operation result comes out to be 00000000 which is the not the value of Oval shape which means we don't have that shape.

**4th test**:

** 00000011****And 00001000**

-------------------------

00000000

i.e the and operation result comes out to be 00000000 which is the not the value of triangle shape which means we don't have that shape.

**Summary**

-------------------------

00000011

ie. now the sample.AllowedShapes contains 00000011 value in it. Next move to the operations which we did on this property .

if((sample.AllowedShapes & Shapes.Circle) == Shapes.Circle) { Console.WriteLine("We have circle shape with us"); }Here we tested whether the AllowedShapes property contains the circle shape or not. The following opertaion took place:

-------------------------

00000001 (Circle)

i.e the and operation result comes out to be 00000001 which is the value of Circle shape which means we have that shape.

if ((sample.AllowedShapes & Shapes.Square) == Shapes.Square) { Console.WriteLine("We have square shape with us"); }Here we tested whether the AllowedShapes property contains the square shape or not. The following opertaion took place:

-------------------------

00000010 (Square)

i.e the and operation result comes out to be 00000010 which is the value of Square shape which means we have that shape.

if ((sample.AllowedShapes & Shapes.Oval) == Shapes.Oval) { Console.WriteLine("We have oval shape with us"); }Here we tested whether the AllowedShapes property contains the oval shape or not. The following opertaion took place:

-------------------------

00000000

i.e the and operation result comes out to be 00000000 which is the not the value of Oval shape which means we don't have that shape.

if ((sample.AllowedShapes & Shapes.Triangle) == Shapes.Triangle) { Console.WriteLine("We have triangle shape with us"); }Here we tested whether the AllowedShapes property contains the triangle shape or not. The following opertaion took place:

-------------------------

00000000

i.e the and operation result comes out to be 00000000 which is the not the value of triangle shape which means we don't have that shape.

That's it above we discussed when and where we should use flags attribute on enums and we figured out how the enumerations constants values are treated as binary representations. Do let me know your feedback, comments.

## Comments (1)