code projected over woman

C# Tips & Tricks #1 – Get Total Number of Items in Enum

You can get the total number of items defined in an enum in C# using the combination of the Enum.GetNames method and the Length property.

How to get the Total Number of Items in an Enum in C# ?

The static method Enum.GetNames returns an array that represents the names of all the items that is present in the enum. You can apply the Length property which retrieves the number of items present.

using System;
namespace DeveloperPublishCsharpSample
{
    public enum Actors
    {
        Ajith,
        Vijay,
        Surya
    }
    class Program
    {
        static void Main(string[] args)
        {
            var totalMembersInEnum = Enum.GetNames(typeof(Actors)).Length;
            Console.WriteLine(totalMembersInEnum);
            Console.ReadLine();
        }
    
    }
  
}

Leave A Reply

Your email address will not be published. Required fields are marked *

You May Also Like

C# Compiler Error CS0442 – ‘Property’: abstract properties cannot have private accessors Reason for the Error You’ll get this error...
This is a really simple one . Below is a simple example of an enum called “Designation” defined with the...
This blog post explain the usage of the Checked Block in .NET and how you can use them in Visual...