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.

Leave a Reply

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

Liked this content?. Please spread the word :)

  • RSS
  • Twitter
    Visit Us
    Follow Me