title: Switch on Enum (with Flags attribute) without declaring every possible combination?
body: <p>how do i switch on an enum which have the flags attribute set (or more precisely is used for bit operations) ?</p>
  
  <p>I want to be able to hit all cases in a switch that matches the values declared.</p>
  
  <p>The problem is that if i have the following enum</p>
  
  <pre><code>[Flags()]public enum CheckType
  {
      Form = 1,   
      QueryString = 2,
      TempData = 4,
  }
  </code></pre>
  
  <p>and I want to use a switch like this  </p>
  
  <pre><code>switch(theCheckType)
  {
     case CheckType.Form:
         DoSomething(/*Some type of collection is passed */);
         break;
  
     case CheckType.QueryString:
         DoSomethingElse(/*Some other type of collection is passed */);
         break;
  
     case CheckType.TempData
         DoWhatever(/*Some different type of collection is passed */);
         break;
  }
  </code></pre>
  
  <p>If "theCheckType" is set to both CheckType.Form | CheckType.TempData I want it to hit both case's. Obviously it wont hit both in my example because of the break, but other than that it also fails because CheckType.Form is not equal to CheckType.Form | CheckType.TempData</p>
  
  <p>The only solution then as I can see it is to make a case for every possible combination of the enum values ?</p>
  
  <p>Something like</p>
  
  <pre><code>    case CheckType.Form | CheckType.TempData:
          DoSomething(/*Some type of collection is passed */);
          DoWhatever(/*Some different type of collection is passed */);
          break;
  
      case CheckType.Form | CheckType.TempData | CheckType.QueryString:
          DoSomething(/*Some type of collection is passed */);
          DoSomethingElse(/*Some other type of collection is passed */);
          break;
  
  ... and so on...
  </code></pre>
  
  <p>But that really isnt very desired (as it will quickly grow very big)</p>
  
  <p>Right now i have 3 If conditions right after eachother instead</p>
  
  <p>Something like </p>
  
  <pre><code>if ((_CheckType &amp; CheckType.Form) != 0)
  {
      DoSomething(/*Some type of collection is passed */);
  }
  
  if ((_CheckType &amp; CheckType.TempData) != 0)
  {
      DoWhatever(/*Some type of collection is passed */);
  }
  
  ....
  </code></pre>
  
  <p>But that also means that if i have an enum with 20 values it have to go through 20 If conditions every single time instead of "jumping" to only the needed "case"/'s as when using a switch.</p>
  
  <p>Is there some magic solution to solve this problem?</p>
  
  <p>I have thought of the possibility to loop through the declared values and then use the switch, then it would only hit the switch for each value declared, but I don't know how it will work and if it performance vice is a good idea (compared to a lot of if's) ?</p>
  
  <p>Is there an easy way to loop through all the enum values declared ?</p>
  
  <p>I can only come up with using ToString() and splitting by "," and then loop through the array and parse every single string.</p>
  
  <hr>
  
  <p>UPDATE:</p>
  
  <p>I see that i haven't done a good enough job explaining.
  My example is to simple (tried to simplify my scenario).</p>
  
  <p>I use it for a ActionMethodSelectorAttribute in Asp.net MVC to determine if a method should be available when resolving the url/route.</p>
  
  <p>I do it by declaring something like this on the method</p>
  
  <pre><code>[ActionSelectorKeyCondition(CheckType.Form | CheckType.TempData, "SomeKey")]
  public ActionResult Index()
  {
      return View();
  } 
  </code></pre>
  
  <p>That would mean that it should check if the Form or TempData have a key as specified for the method to be available. </p>
  
  <p>The methods it will be calling (doSomething(), doSomethingElse() and doWhatever() in my previous example) will actually have bool as return value and will be called with a parameter (different collections that doesn't share a interface that can be used - see my example code in the link below etc).</p>
  
  <p>To hopefully give a better idea of what i am doing i have pasted a simple example of what i am actually doing on pastebin - it can be found here <a href="http://pastebin.com/m478cc2b8" rel="noreferrer">http://pastebin.com/m478cc2b8</a></p>