Total number of items defined in an enum C#

Total Post:9

Points:63
C# 
C#
 390  View(s)
Ratings:
Rate this:

I want to count the number. How can I get the number of items defined in an enum?

enum MyEnum

{
    A = 1,
    B = 2,
    C = 1,
    D = 3,
    E = 2
}
  1. Post:5

    Points:35
    Re: Total number of items defined in an enum C#

    For this You can use the static method Enum.GetNames 

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

Answer

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!