The blog above has a very useful bit of code for C#.Net that will convert a Enum definition into a List. This can be very useful, for instance to place all the values of an enum into a drop down list. I used it to check a cast from an int to enum was valid by checking it against the list.
public static List<T> EnumToList<T>(){Type enumType = typeof(T);if (enumType.BaseType != typeof(Enum))throw new ArgumentException("T must be of type System.Enum");} |
Then used it like this to check a valid enum:
List<DayOfWeek> weekdays = EnumToList<DayOfWeek>()int sunday = 6;DayOfWeek day = (DayOfWeek)sunday;
|
This is useful because the int to enum cast will cast to an enum even if it doesn’t exist. So its best ot make sure its valid before you continue to use it.
Kudos to the guys in the blog above for working out the code.




