1

I need to know the number of enums that have been declared. So far I am using the following which works but I wonder if there is a better way?

 enum MyEnum 
 {
  foo = 1,
  bar = 2
 }

int noOfEnums = Enum.GetNames(typeof(MyEnum)).Count();

noOfEnums will be 2;

user2425056
  • 327
  • 2
  • 4
  • 14

1 Answers1

1

You may try to use:

enum MyEnum 
{
  foo = 1,
  bar = 2
}

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

The length property of this array equals the number of items defined in the enum

Also check Count Items in a C# Enum

Rahul Tripathi
  • 168,305
  • 31
  • 280
  • 331