Total number of items defined in an enum

SO User picture SO User · May 13, 2009 · Viewed 233.3k times · Source

How can I get the number of items defined in an enum?

Answer

Kasper Holdum picture Kasper Holdum · May 13, 2009

You can use the static method Enum.GetNames which returns an array representing the names of all the items in the enum. The length property of this array equals the number of items defined in the enum

var myEnumMemberCount = Enum.GetNames(typeof(MyEnum)).Length;