title: Building generic method for List<Enum>
body: <p>In my application, I have something like:</p>
  
  <pre><code>public enum Locations {
    LocationA,
    LocationB,
    LocationC
  }
  
  private List&lt;Locations&gt; _myLocations;
  
  public Int64 PackedLocations {
    get {
      return PackEnumList(this._myLocations);
    }
  }
  </code></pre>
  
  <p>So: an enum (backed by int), a List of those enum values, and a read-only property which returns the result of a method I've left out so far.</p>
  
  <p>That method, PackEnumList, is meant to give me a 64-bit integer where each BIT denotes whether the corresponding enum value was selected or not in a list of unique enum values. So in my example above, if _myLocations has only one item: {Locations.LocationA}, the result would be 1 (binary: ...00001), if we then add Locations.LocationC to that list, the result would be 5 (binary: ...000101). The implementation isn't important right now (but I'll include it below for completion/interest/feedback), but the signature of that method is:</p>
  
  <pre><code>public Int64 PackEnumList(List&lt;Enum&gt; listOfEnumValues) {
     ...
  }
  </code></pre>
  
  <p><strong>When I compile, I get an error that "the best overloaded method ... has some invalid arguments".</strong> </p>
  
  <p>I'm guessing this is because _myLocations is being seen as a List of int values, but I'd like PackEnumList() to work even if the enumeration being used were backed by something else, if possible.</p>
  
  <p>Is there a more appropriate way to make a method which will accept a List/Collection of any enumeration?</p>
  
  <p>For completeness, here's the rest of what I'm trying to do (these are static because they're in a shared utility class). These are completely untested yet (since I can't get past the compile error when calling the pack method), so take them with a grain of salt. And there might be a better way to do this, I'm doing this half to solve an interesting problem, and half because I think it is an interesting problem.</p>
  
  <pre><code>    public static Int64 PackEnumList(List&lt;Enum&gt; listOfEnumValues) {
          BitArray bits = new BitArray(64, defaultValue: false);
          foreach (var value in listOfEnumValues) {
              // get integer value of each Enum in the List:
              int val = Convert.ToInt32(value);
              if (val &gt;= 64) {
                  // this enum has more options than we have bits, so cannot pack
                  throw new Exception("Enum value out of range for packing: " + val.ToString());
              }
              bits[val] = true;
          }
          var res = new Int64[1];
          bits.CopyTo(res, 0);
          return res[0];
      }
  
      // (this method is a little farther from the ideal: the resulting list will need
      //     to be matched by the caller to the appropriate List of Enums by casting 
      //     each Int32 value to the Enum object in the list)
      public static List&lt;Int32&gt; UnpackEnumList(Int64 packedValue) {
          string binaryString = Convert.ToString(packedValue, 2);
          List&lt;Int32&gt; res = new List&lt;Int32&gt;();
          for (int pos = 0; pos &lt; binaryString.Length; pos++) {
              if (binaryString[binaryString.Length - pos - 1] == '1') {
                  // bit is on
                  res.Add(pos);
              }
          }
          return res;
      }
  </code></pre>