title: Extension Method to Get the Values of Any Enum
body: <p>I've been trying to create an extension method, that would work on any enum, to return its values.</p>
  
  <p>Instead of doing this:</p>
  
  <pre><code>Enum.GetValues(typeof(BiasCode)).Cast&lt;BiasCode&gt;()
  </code></pre>
  
  <p>It would be nice to do this:</p>
  
  <pre><code>new BiasCode().Values()
  </code></pre>
  
  <p>It would even be better without <em>new</em>, but that's another issue.</p>
  
  <p>I have a <a href="https://dotnetfiddle.net/GSnVBD" rel="noreferrer">.NET fiddle</a> that has a solution that's close (code shown below). The problem with this code is that the extension method is returning <code>List&lt;int&gt;</code>. I would like to have it return a list of the enum values itself. Returning <code>List&lt;int&gt;</code> isn't terrible; it just means I have to cast the result.</p>
  
  <p>Is it even possible to do this? I tried making the extension method generic, but ran into problems. This is as close as I was able to get:</p>
  
  <pre><code>using System;
  using System.Linq;
  using System.Collections.Generic;
  
  public class Program
  {
      public static void Main()
      {
          foreach (int biasCode in new BiasCode().Values())
          {
              DisplayEnum((BiasCode)biasCode);
          }
      }
  
      public static void DisplayEnum(BiasCode biasCode)
      {
          Console.WriteLine(biasCode);    
      }
  }
  
  public enum BiasCode
  {
      Unknown,
      OC,
      MPP
  }
  
  public static class EnumExtensions
  {
      public static List&lt;int&gt; Values(this Enum theEnum)
      {
          var enumValues = new List&lt;int&gt;();
          foreach (int enumValue in Enum.GetValues(theEnum.GetType()))
          {
              enumValues.Add(enumValue);
          }
  
          return enumValues;
      }
  }
  </code></pre>