How to get the total number of items in a Enum in C# ?


This content was proof-read by Grammarly. You can Try Grammary for FREE

If you want to get the total number of items that is defined in an enum using C# , you can use the Enum.GetNames static method combined with the Length property.

How to get the total number of items in a Enum in C# ?

Here’s a code sample demonstrating how to do it.

using System;

namespace AbundantcodeConsoleApp
{
    class Program
    {
        public enum EmploymentType
        {
            Permament,
            Contract,
            Intern
        }
        static void Main(string[] args)
        {
            var NoOfItems = Enum.GetNames(typeof(EmploymentType)).Length;
            Console.WriteLine(NoOfItems.ToString());
            Console.ReadLine();
        }
    }
}

This would display 3 in the console.

%d bloggers like this: