HiveBrain v1.2.0
Get Started
← Back to all entries
patterncsharpCritical

Total number of items defined in an enum

Submitted by: @import:stackoverflow-api··
0
Viewed 0 times
totalenumdefinednumberitems

Problem

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

Solution

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;

Code Snippets

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

Context

Stack Overflow Q#856154, score: 609

Revisions (0)

No revisions yet.