C# Enum GetValues Method

The Enum.GetValues() method in C# retrieves an array containing the values of all constants in a specified enumeration. This method is useful when you need to iterate through all enum values dynamically or perform operations on the entire set of enum values.

Syntax

Following is the syntax for the Enum.GetValues() method −

public static Array GetValues(Type enumType)

Parameters

  • enumType − The Type of the enumeration whose values you want to retrieve.

Return Value

Returns an Array containing the values of the constants in the specified enumeration.

Using GetValues() to Retrieve Enum Values

Here's how to use GetValues() to get all values from an enum −

Example

using System;

public class Demo {
   enum Rank { Jack = 10, Tom = 19, Tim = 26 };
   
   public static void Main() {
      Console.WriteLine("Here are the university rank of MCA Students College ABC:");
      foreach(int res in Enum.GetValues(typeof(Rank))) {
         Console.WriteLine(res);
      }
   }
}

The output of the above code is −

Here are the university rank of MCA Students College ABC:
10
19
26

Using GetValues() with Different Enum Types

Example

using System;

public class EnumExample {
   enum Days { Monday = 1, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday };
   enum Colors { Red, Green, Blue, Yellow };
   
   public static void Main() {
      Console.WriteLine("Days enum values:");
      foreach(int day in Enum.GetValues(typeof(Days))) {
         Console.WriteLine(day);
      }
      
      Console.WriteLine("\nColors enum values:");
      foreach(int color in Enum.GetValues(typeof(Colors))) {
         Console.WriteLine(color);
      }
   }
}

The output of the above code is −

Days enum values:
1
2
3
4
5
6
7

Colors enum values:
0
1
2
3

Getting Enum Names Along with Values

You can combine GetValues() with GetNames() to display both names and values −

Example

using System;

public class EnumDisplay {
   enum Status { Active = 100, Inactive = 200, Pending = 300 };
   
   public static void Main() {
      string[] names = Enum.GetNames(typeof(Status));
      int[] values = (int[])Enum.GetValues(typeof(Status));
      
      Console.WriteLine("Status Enum:");
      for(int i = 0; i < names.Length; i++) {
         Console.WriteLine($"{names[i]} = {values[i]}");
      }
   }
}

The output of the above code is −

Status Enum:
Active = 100
Inactive = 200
Pending = 300

Conclusion

The Enum.GetValues() method provides a convenient way to retrieve all values from an enumeration as an array. This method is particularly useful for dynamic enum processing, validation, or when you need to iterate through all possible enum values at runtime.

Updated on: 2026-03-17T07:04:35+05:30

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements