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

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

You May Also Like

This C# program calculates and displays an upper triangular matrix based on user input. Problem Statement: The program takes the...
This C# program serves as a demonstration of bitwise operators, which are fundamental operators used for manipulating individual bits in...
This C# program is designed to interchange or swap the columns of a matrix. A matrix is a two-dimensional array...