C# Enum GetValues Method

CsharpProgrammingServer Side Programming

Get the array of the values of the constants in a specified enumeration.

Here is our enum.

enum Rank { Jack = 10, Tom = 19, Tim = 26 };

Now, get all the values of the enum as an array and display using GetValues() method.

foreach(int res in Enum.GetValues(typeof(Rank))) {
   Console.WriteLine(res);
}

Let us see the entire example.

Example

 Live Demo

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);
      }
   }
}

Output

Here are the university rank of MCA Students College ABC:
10
19
26
raja
Updated on 23-Jun-2020 09:18:16

Advertisements