Given an enum like this:
public enum City {
London = 1,
Liverpool = 20,
Leeds = 25
}
public enum House {
OneFloor = 1,
TwoF
Why not:
IEnumerable
So you can use:
var result = GetValues();
If you would like to make constraint generic T
as enum
, because enum
cannot be used as generic contraint directly, but enum inherits from interface IConvertible
, believe this way is okay:
IEnumerable
To replace IEnumerable
by Dictionary
:
Dictionary GetValues() where T : struct, IConvertible
{
return Enum.GetValues(typeof (T)).Cast()
.ToDictionary(value => Convert.ToInt32(value),
value => value.ToString());
}
Edit: As Magnus's comment, if you need to make sure the order of items, Dictionary is not the option. Define your own strong type would be better.