How can I use Generics to create a way of making an IEnumerable from an enum?
Posted
by
Samantha J
on Stack Overflow
See other posts from Stack Overflow
or by Samantha J
Published on 2012-09-16T14:41:46Z
Indexed on
2012/09/16
15:38 UTC
Read the original article
Hit count: 220
c#
Given an enum like this:
public enum City {
London = 1,
Liverpool = 20,
Leeds = 25
}
public enum House {
OneFloor = 1,
TwoFloors = 2
}
I am using the following code to give me an IEnumerable:
City[] values = (City[])Enum.GetValues(typeof(City));
var valuesWithNames = from value in values
select new { value = (int)value, name = value.ToString() };
The code works very good however I have to do this for quite a lot of enums. Is there a way I could create a generic way of doing this?
© Stack Overflow or respective owner