Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to calculate the total number of items defined in an enum in C#?
An enum is a special value type that represents a group of named constants. When working with enums, you often need to know how many items are defined in the enum. C# provides several methods to calculate the total count of enum items.
Syntax
Following are the main approaches to get the total count of enum items −
// Using Enum.GetNames() int count = Enum.GetNames(typeof(EnumName)).Length; // Using Enum.GetValues() int count = Enum.GetValues(typeof(EnumName)).Length;
Using Enum.GetNames() Method
The Enum.GetNames() method returns an array of string names for all enum values. The Length property gives the total count −
using System;
class Program {
enum Level {
Low,
Medium,
High
}
public static void Main() {
int myCount = Enum.GetNames(typeof(Level)).Length;
Console.WriteLine("Total enum items: " + myCount);
// Display all enum names
string[] names = Enum.GetNames(typeof(Level));
Console.WriteLine("Enum names:");
foreach (string name in names) {
Console.WriteLine("- " + name);
}
}
}
The output of the above code is −
Total enum items: 3 Enum names: - Low - Medium - High
Using Enum.GetValues() Method
The Enum.GetValues() method returns an array of all enum values. This approach is useful when you need both the count and the actual values −
using System;
class Program {
enum Priority {
Low = 1,
Medium = 5,
High = 10,
Critical = 20
}
public static void Main() {
int count = Enum.GetValues(typeof(Priority)).Length;
Console.WriteLine("Total enum items: " + count);
// Display all enum values
Priority[] values = (Priority[])Enum.GetValues(typeof(Priority));
Console.WriteLine("Enum values:");
foreach (Priority value in values) {
Console.WriteLine(value + " = " + (int)value);
}
}
}
The output of the above code is −
Total enum items: 4 Enum values: Low = 1 Medium = 5 High = 10 Critical = 20
Comparison of Methods
| Method | Returns | Use Case |
|---|---|---|
| Enum.GetNames() | Array of string names | When you need the names of enum items |
| Enum.GetValues() | Array of enum values | When you need both count and actual values |
Using with Generic Method
You can create a generic method to get the count of any enum type −
using System;
class Program {
enum Status { Active, Inactive, Pending }
enum Color { Red, Green, Blue, Yellow }
public static int GetEnumCount<T>() where T : Enum {
return Enum.GetNames(typeof(T)).Length;
}
public static void Main() {
Console.WriteLine("Status enum count: " + GetEnumCount<Status>());
Console.WriteLine("Color enum count: " + GetEnumCount<Color>());
}
}
The output of the above code is −
Status enum count: 3 Color enum count: 4
Conclusion
To calculate the total number of items in a C# enum, use Enum.GetNames(typeof(EnumName)).Length or Enum.GetValues(typeof(EnumName)).Length. Both methods return the same count, but GetNames() is typically preferred when you only need the count, while GetValues() is useful when you also need to work with the actual enum values.
