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 codeIn 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
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"); }
if ((sample.AllowedShapes & Shapes.Triangle) == Shapes.Triangle) { Console.WriteLine("We have triangle shape with us"); }
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.
What kind of email newsletter would you prefer to receive from CodeAsp.Net?18