SiteMap  | About    

Enum Flags Explained


An Enum (Enumeration) type provides an efficient way to define a set of named integral constants that may be assigned to a variable.

enum Grade { Low, Medium, High, Maximum };

Enum Flags Attribute

What Does the [Flags] Attribute Really Do c#

The idea of Enum Flags is to take an enumeration variable and allow it hold multiple values. It should be used whenever the enum represents a collection of flags, rather than representing a single value. Such enumeration collections are usually manipulated using bitwise operators. You create a bit flags enum by applying the System.FlagsAttribute attribute and defining the values appropriately, so that AND, OR, NOT and XOR bitwise operations can be performed on them.


[Flags] enum Grade { Low = 1, Medium = 2, High = 4, Maximum = 8 }


<Flags> _ Enum Grade Low = 1 Medium = 2 High = 4 Maximum = 8 End Enum

You can use like this:

Grade eGrade = Grade.Medium Grade.High;

In conditions:

if((mySchool.AllowedGrade & Grade.High) == Grade.High) { // Code Here }

c# Create Enumeration as Bit Flags

To properly implement an enum as a flag, you need to have the values increasing by a power of two (2). You should define enumeration constants in powers of two, that is, 1, 2, 4, 8, and so on. It is important, because it allows us to do bit-wise operations on the number. The values themselves can easily be calculated by raising 2 to the power of a zero-based sequence of numbers like the following:

Combining Enum Values with Bit-Flags c#

yields this (yes, 2 to the zero power is one):

1, 2, 4, 8, 16, 32, 64

This means the individual flags in combined enumeration constants do not overlap.

Unlike the old enums, the actual values in a flags enumerator must be in sequence for it to work correctly. This does not refer to a base 10 sequence, but instead to a base 2 sequence. Hence the Character enumeration can now act in Bitwise Operators. The values of the Enum will look like the following:

00000000 0 00000001 1 00000010 2 00000100 4 00001000 16 00010000 32 00100000 64 01000000 128

What it is explain that the integer value expressed as base 2 must be as shown (for four values in the above example): 00001 00010 00100 01000 . Expressed in base 10, this is 1, 2, 4, 8.

Difference :

You can see the difference lies in the Enum.ToString() method. If your enum has the [Flags] attribute set then the ToString() will return a comma seperated values list of bitwise enum values. If there's no [Flags] attribute ToString() will return a number for every bitwise value.

C# Source Code

using System; using System.Windows.Forms; namespace WindowsFormsApplication1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } enum Grade { Low = 1, Medium = 2, High = 4, Maximum = 8 } [Flags] enum GradeFlags { Low = 1, Medium = 2, High = 4, Maximum = 8 } private void button1_Click(object sender, EventArgs e) { string iGrade = (Grade.Low Grade.High ).ToString(); string fGrade = (GradeFlags.Low GradeFlags.High).ToString(); MessageBox.Show(iGrade + " - with flag : " + fGrade); } } }

Output : 5 - with flag : Low, High

VB.Net Source Code

Public Class Form1 Enum Grade Low = 1 Medium = 2 High = 4 Maximum = 8 End Enum <Flags()> _ Enum GradeFlags Low = 1 Medium = 2 High = 4 Maximum = 8 End Enum Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim iGrade As String = (Grade.Low Or Grade.High).ToString() Dim fGrade As String = (GradeFlags.Low Or GradeFlags.High).ToString() MessageBox.Show(iGrade & " - with flag : " & fGrade) End Sub End Class

Output : 5 - with flag : Low, High


C# use of enum flag

Although C# always allows developers to perform bit operations on enumeration without the FlagsAttribute, Visual Basic (VB.Net) does not. So if you are exposing types to other programming languages, then marking enumeration with the FlagsAttribute is a good idea; it also makes it clear that the members of the enumeration are designed to be used together.

Enum Basics

Enums are strongly typed constants which makes the code more readable and less prone to errors. It is useful when you have a set of values that are functionally significant and unchanged. More about.... Enum in C#

Convert an enum to a List

The following program shows how to convert an enum to List Datastructure. More about.... Enum to List (C) 2016    Founded by raps mk
All Rights Reserved. All other trademarks are property of their respective owners.