Use Enums for Bitwise Operation with Flags attribute
Oct 3, 2008

You may want to tag your enums with [Flags] should you need to tackle different permutation/combination using bitwise operation.Find the code below I put together quick as a sample…
class Test
{
[Flags]
public enum AccessRightsStates
{
Read = 1 << 0, //1,
Edit = 1 << 1, //2,
Publish = 1 << 2, //4,
Admin = 1 << 3 //8
}
static void Main(string[] args)
{
int combinedState = (int)(AccessRightsStates.Read | AccessRightsStates.Edit | AccessRightsStates.Admin);
Console.WriteLine("Combined State value = {0}", combinedState.ToString());
Console.WriteLine("Read state is {0}", ContainsState((AccessRightsStates)combinedState, AccessRightsStates.Read));
Console.WriteLine("Edit state is {0}", ContainsState((AccessRightsStates)combinedState, AccessRightsStates.Edit));
Console.WriteLine("Publish state is {0}", ContainsState((AccessRightsStates)combinedState, AccessRightsStates.Publish));
Console.WriteLine("Admin state is {0}", ContainsState((AccessRightsStates)combinedState, AccessRightsStates.Admin));
Console.ReadLine();
}
public static bool ContainsState(AccessRightsStates combinedstate, AccessRightsStates againststate)
{
return ((combinedstate & againststate) == againststate);
}
}

Prakash Software Solutions PVT LTD. Offer wide range of services to their clients all over the globe. We successfully delivered projects i.e. software development, cloud computing solutions, SharePoint development solutions, mobile app development and Microsoft technology solution to our Sweden, Denmark, Netherlands, France, Norway, Germany, Spain, Italy, Switzerland, Belgium, Luxembourg, Ireland, United Kingdom, United States, Canada, Brazil, Peru, UAE, Dubai, Saudi Arabia, Kuwait, Qatar, Bahrain, Singapore, New Zealand and Australia Clients.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

Related Posts

Entity Framework 7 Beta Version 8 Launched

What is Entity Framework? Entity framework is an ORM that generates business objects…

Jun 2, 2016

Covariance and Contra variance in .Net 4.0 Framework

Covariance and Contra variance in Delegates There has always been confusion for me…

Jul 8, 2010

How to open large XML files

Problem: Open large xml files without loading the whole xml file in memory…

Jun 9, 2010
Top

Copyright © Prakash Software Solutions Pvt. Ltd. All Rights Reserved.