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
  • 319
  • 3
  • 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
  • 161,154
  • 30
  • 262
  • 319