Enum.GetValues() Return Type

C#Enums

C# Problem Overview


I have read documentation that states that ‘given the type of the enum, the GetValues() method of System.Enum will return an array of the given enum's base type’ ie int, byte etc

However I have been using the GetValues method and all I keep getting back is an array of type Enums. Am I missing something??


public enum Response
{
Yes = 1,
No = 2,
Maybe = 3
}




foreach (var value in Enum.GetValues(typeof(Response)))
{
var type = value.GetType(); // type is always of type Enum not of the enum base type
}

Thanks

C# Solutions


Solution 1 - C#

You need to cast the result to the actual array type you want

(Response[])Enum.GetValues(typeof(Response))

as GetValues isn't strongly typed

EDIT: just re-read the answer. You need to explicitly cast each enum value to the underlying type, as GetValues returns an array of the actual enum type rather than the base type. Enum.GetUnderlyingType could help with this.

Solution 2 - C#

If you're using NET 3.5 (i.e. you have LINQ) you can do:

var responses = Enum.GetValues(typeof(Response)).Cast<Response>();

Solution 3 - C#

Personally I've created a separate method in my Utils project, which I include in my other projects. Here's the code I use:

public static class EnumUtil
{
    public static IEnumerable<TEnum> GetAllValues<TEnum>() 
        where TEnum : struct, IConvertible, IComparable, IFormattable
    {
        return Enum.GetValues(typeof(TEnum)).Cast<TEnum>();
    }   
}

And I call it like this:

var enumValues = EnumUtil.GetAllValues<Response>();

Solution 4 - C#

Can you please refer to the documentation you mention. The documentation on Enum.GetValues does not mention anything like that (quote from that page):

> Return Value > > Type: System.Array > > An Array of the > values of the constants in enumType. > The elements of the array are sorted > by the binary values of the > enumeration constants.

Solution 5 - C#

As Roger mentioned in a comment, it would be nice if there was a Enum.GetValues<MyEnum>() generic implementation, but there is not.

This problem annoyed the heck out of me, as well, so I created a library in C++/CLI that has generic implementations of all of the static methods on the Enum class (as well as a bunch of other generic methods for working with enums).

The library is written in C++/CLI because C# does not support constraining a generic type by System.Enum. C++/CLI (and the CLR) do support constraining by System.Enum and C#/VB.NET has no problem understanding calls to a method that have this constraint.

In the case of your example, you'd use Enums.GetValues<MyEnumType>() which will hand you an array of MyEnumType without the need to cast. Though C# and VB.Net do not support defining an enum constraint, they have no problem with consuming a method/class that has such a constraint and intellisense/the compiler handle it perfectly.

Solution 6 - C#

Similar to Joel's Answer but done a slight different way:

public static class Enums<T>
  where T : struct, IComparable, IFormattable, IConvertible
{
  static Enums()
  {
    if (!typeof(T).IsEnum)
      throw new ArgumentException("Type T must be an Enum type");  
  }

  public static IEnumerable<T> GetValues()
  {
    var result = ((T[])Enum.GetValues(typeof(T)).ToList()

    return result;
  }
}

Usage:

IEnumerable<System.Drawing.FontStyle> styles = Enums<System.Drawing.FontStyle>.GetValues();

Solution 7 - C#

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionCraglyView Question on Stackoverflow
Solution 1 - C#thecoopView Answer on Stackoverflow
Solution 2 - C#cdmckayView Answer on Stackoverflow
Solution 3 - C#JoelView Answer on Stackoverflow
Solution 4 - C#Fredrik MörkView Answer on Stackoverflow
Solution 5 - C#mdipView Answer on Stackoverflow
Solution 6 - C#Erik PhilipsView Answer on Stackoverflow
Solution 7 - C#juFoView Answer on Stackoverflow