Ehsan Ghanbari

Experience, DotNet, Solutions

Why the values of enums are bit flag in .Net?

Take a look at AttributeTargets in System namespace:

 

namespace System
{
    [Serializable]
    [ComVisible(true)]
    [Flags]
    public enum AttributeTargets
    {
             Assembly = 1,
             Module = 2,
             Class = 4,
             Struct = 8,
             Enum = 16,
             Constructor = 32,
             Method = 64,
            Property = 128,
            Field = 256,
            Event = 512,
            Interface = 1024,
            Parameter = 2048,
            Delegate = 4096,
            ReturnValue = 8192,
            GenericParameter = 16384,
            All = 32767,
    }
}

 

Have you ever thought about that why these values assigned to 1, 2, 4, 8, 16,… ?? If you remember bit flag pattern, numbers look like this:

  1. 00000000  0
  2. 00000001  1
  3. 00000010  2
  4. 00000100  4
  5. 00001000  16
  6. 00010000  32
  7. 00100000  64
  8. 01000000  128

So, what's the benefit of this? They use these number to avoid the combination of enum members, especially when using bitwise operation! Imagine that you have written your  own enum like this:

 

    public enum AccountState
    {
        Closed,
        Blocked,
        Active,
        Forbidden
    }

 

By default, enum will create the integer numbers: 0,1,2,3,4,5,6,…  now when you want to use this attribute somewhere like this:

 

    Public class BitwiseClass
    {
        public void Method()
        {
            Account account = new Account();

            account.AccountState = AccountState.Active | AccountState.Blocked | AccountState.Forbidden |
                                   AccountState.Forbidden;
        }
    }

    public class Account
    {
       public AccountState AccountState { get; set; }
    }

 

In Method(), when you want to do the bitwise operation, the combination of 1 and 2 is 3, 0 and 1 is 1, 0 and 2 is 2,… So in this case system will combine these numbers! But in bit flag operation, numbers will look like this:

 

   public enum AccountState
    {
        Closed,          // 0000
        Blocked,       //  0001
        Active,          //  0010
        Forbidden   //   0100
    }

 

The combination of these number never will be iterated. By you don't need to assign these numbers in every enum! You just need to use Flags attribute:

 

    [Flags]
    public enum AccountState
    {
        Closed,
        Blocked,
        Active,
        Forbidden
    }

 

About Me

Ehsan Ghanbari

Hi! my name is Ehsan. I'm a developer, passionate technologist, and fan of clean code. I'm interested in enterprise and large-scale applications architecture and design patterns and I'm spending a lot of my time on architecture subject. Since 2008, I've been as a developer for companies and organizations and I've been focusing on Microsoft ecosystem all the time. During the&nb Read More

Post Tags
Pending Blog Posts
Strategic design
Factory Pattern
time out pattern in ajax
Selectors in Jquery
Peridic pattern
How to use PagedList In asp.net MVC
Redis as a cache server
Domain driven design VS model driven architecture
How to query over Icollection<> of a type with linq
What's the DDD-lite?
comments powered by Disqus