In this article, we talk about enums and, more specifically, how to find the total number of items defined in the enum. But first of all, let’s define an enum.

To download the source code for this article, you can visit our GitHub repository.

Let’s start.

What is an Enum

An enum, short for enumeration, serves as a lightweight data structure within a programming language. It encapsulates a set of named values corresponding to a distinct group of related constants. The primary purpose of enums is to enhance code readability and maintainability by providing human-readable labels for specific constant values. To delve more into enums, you can read our article that covers that topic.

Support Code Maze on Patreon to get rid of ads and get the best discounts on our products!
Become a patron at Patreon!

Let’s create our first enum:

public enum Seasons
{
    Summer,
    Autumn,
    Winter,
    Spring 
}

Here we create an enumeration, named Seasons, containing the season’s names.

Find the Total Items Defined in an Enum

Before answering our question, we must mention a few words about two important methods. These are the GetNames() and GetValues() methods. Both belong to the Enum class. The GetNames() method returns an array of strings with the names of constants. On the other hand, 
GetValues() returns an array of the values of the constants. When we talk about constants, we refer to the individual values that are defined within an enum type.

Now, let’s see an example of the GetValues() method:

var names = Enum.GetValues<Seasons>();
foreach (var n in names)
{
    Console.WriteLine(n);
}

We must emphasize that the GetValues() method displays all the values of constants if they are different from each other. Otherwise, if multiple items have the same constant, then only the first item is part of the return.

Let’s check the output of our code:

Summer
Autumn
Winter
Spring

It is a good opportunity to emphasize that an enum has default values that are unique if we do not set custom values for it. That is why we have in our example all the elements in the output. We will see an example later with custom values.

Now that we have some knowledge of enums, let’s see how we can find the total number of items in them. We use the Length statement for this purpose:

var getNames = Enum.GetNames<Seasons>().Length;
var getValues = Enum.GetValues<Seasons>().Length;
Console.WriteLine("Total items by GetNames: " + getNames);
Console.WriteLine("Total items by GetValues: " + getvalues);

The above statement applies to both methods we talk about. More specifically with the command Enum.GetNames<Seasons>().Length and Enum.GetValues<Seasons>().Length we get the total number of elements in all the dimensions of our structure. 

So, let’s verify this by checking the output:

Total items by GetNames: 4
Total items by GetValues: 4

As we see in both cases, we get the total number of elements inside the enum.

It is important to mention about GetValues() that the total number does not depend on the uniqueness of the constants here.

Let’s go one step further and expand our knowledge by finding the number of distinct values ​​in an enum. Let’s create a new one with custom values at this time:

public enum Medals
{
    Gold=1,
    Silver=2,
    Bronze=2    
}

As we can see, we set values for each element, and two of them (Silver, Bronze) have the same value. We do this on purpose.

With that done, we can check how many different values exist in the enum:

var distinctValues = Enum.GetValues(typeof(Medals)).Cast<Medals>().Distinct().Count();
Console.WriteLine("Total number of distinct values: " + distinctValues);

With this statement, we can return the total number of distinct values in our enumeration:

Total number of distinct values: 2

Conclusion

In summary, we saw that GetNames().Length and GetValues().Length in C# figures out how many items exist in an enumeration. Additionally, we learned how to find distinct values within an enum.

Liked it? Take a second to support Code Maze on Patreon and get the ad free reading experience!
Become a patron at Patreon!