How can you enumerate an enum
in C#?
E.g. the following code does not compile:
public enum Suit
{
Just by combining the top answers, I threw together a very simple extension:
public static class EnumExtensions
{
///
/// Gets all items for an enum value.
///
///
/// The value.
///
public static IEnumerable GetAllItems(this T value) where T : Enum
{
return (T[])Enum.GetValues(typeof (T));
}
}
It is clean, simple, and, by @Jeppe-Stig-Nielsen's comment, fast.