title: Multiple enums with the same values
body: <p>I have a set of constants for which I use an enum in my C# project. In some situations I need just a single value in other situations I need multiple values. I started out with using IEnumerable or a simple array of my enum type to handle multiple values. However I have recently come to need multiple values in an attribute constructor for multiple attributes which I would like to put in a const field of the class.</p>
  
  <p>A simple example:</p>
  
  <pre><code>public enum Browsers {
    Firefox, Chrome, IE, Opera, Safari
  }
  
  public class SomeClass {
    [Attr(new Browsers[] { Browsers.Firefox, Browsers.Chrome })]
    public void SomeMethod() { }
  
    [Attr(new Browsers[] { Browsers.Firefox, Browsers.Chrome })]
    public void SomeOtherMethod() { }
  }
  </code></pre>
  
  <p>I would like to refactor the array of browsers out into a field in the class, however for a field to be used in an attribute constructor it must be a const field and a field with an array initialization cannot be const.</p>
  
  <p>I then tried to use the FlagsAttribute on my enum so that the enum can hold multiple values. I can then make a field <code>private const Browsers = Browsers.Firefox | Browsers.Chrome</code> and I'm good. In this situation. </p>
  
  <p>Applying the FlagsAttribute thus allowing for multiple values is not so good in a situation where I need only a single value. In those situations I will always have to first check that the enum has only a single value (I do that by checking if it is a power of 2). I find that a bit annoying. Trading maintainability in one situation for less maintainability in other situations - not ideal.</p>
  
  <p>So my question is is there some smart way to get around this? I thought of making two separate enums one with FlagsAttribute and one without however I will then have to maintain all the values of the enums in two places instead of one. Again not ideal.</p>
  
  <p>Have I missed something that can solve this in a way that does not trade off maintainability anywhere?</p>
  
  <p><strong>UPDATE:</strong>
  What I wanted my code to look like is something like this:</p>
  
  <pre><code>public class SomeClass {
    private const Browsers[] browsers = new Browsers[] { Browsers.Firefox, Browsers.Chrome };
  
    [Attr(browsers)]
    public void SomeMethod() { }
  
    [Attr(browsers)]
    public void SomeOtherMethod() { }
  }
  </code></pre>
  
  <p>However that results in a compile error.</p>
  
  <p>An example of a place where I use the enum with a single value only:</p>
  
  <pre><code>public void SomeMethod(Browsers browser) {
    /* When using Flags I need to verify that browser contains only a single value */
    switch(browser)
    {
      case Browsers.Firefox:
        /* do something */
        break;
      case Browsers.Chrome:
        /* do something */
        break;
      /* cases for the rest of the values */
    }
  
  }
  </code></pre>
  
  <p>My question is: can I somehow create two enums with the same values, one of which has the Flags attribute and the other not having it?</p>
  
  <p>Bonus question: is there a smarter way around this that I am just not seeing?</p>