C# | Type.GetEnumValues() Method - GeeksforGeeks

27 Aug, 2021
Type.GetEnumValues() Method is used to return an array of the values of the constants in the current enumeration type.Syntax: 
public virtual Array GetEnumValues ();
Return Value: This method returns an array which contains the values. The elements of the array are sorted by the binary values i.e. the unsigned values of the enumeration constants.Exception: This method will give ArgumentException if the current type is not an enumeration.Below programs illustrate the use of the above-discussed method:Example 1: 
csharp
// C# program to demonstrate the// Type.GetEnumValues() Methodusing System;using System.Globalization;using System.Reflection; class GFG {     // Defining enum ABC    enum ABC { A,               B,               C,               D,               E,               F }     // Main Method    public static void Main()    {         // try-catch block to        // handle exceptions        try {             // Creating and initializing object            // of ABC with instance of enum ABC            ABC a = ABC.A;             // Declaring and initializing            // object of Type            Type type = a.GetType();             // Getting an array of the values            // of the constants            // By using GetEnumValues() method            Array obj = type.GetEnumValues();             // Display values of the constants            Console.Write("Values of the constants is : {0} ", obj);        }         // catch ArgumentException here        catch (ArgumentException e)        {            Console.WriteLine("The current type is not an enumeration.");            Console.Write("Exception Thrown: ");            Console.Write("{0}", e.GetType(), e.Message);        }    }}
Values of the constants is : GFG+ABC[] 
 
Example 2: For ArgumentException
csharp
// C# program to demonstrate the// Type.GetEnumValues() Methodusing System;using System.Globalization;using System.Reflection; class GFG {     // Defining enum ABC    enum ABC { A,               B,               C,               D,               E,               F }     // Main Method    public static void Main()    {         // try-catch block to        // handle exceptions        try {             // Creating and initializing object            // of ABC with instance of enum ABC            ABC a = ABC.A;             // Declaring and initializing            // object of Type            Type type = typeof(int);             // Getting an array of the values            // of the constants            // By using GetEnumValues() method            Array obj = type.GetEnumValues();             // Display values of the constants            Console.Write("Values of the constants is : {0} ", obj);        }         // catch ArgumentException here        catch (ArgumentException e)        {            Console.WriteLine("The current type is not an enumeration.");            Console.Write("Exception Thrown: ");            Console.Write("{0}", e.GetType(), e.Message);        }    }}
The current type is not an enumeration.
Exception Thrown: System.ArgumentException
 
Reference: 
https://docs.microsoft.com/en-us/dotnet/api/system.type.getenumvalues?view=netframework-4.8
 
rs1686740
CSharp-method
CSharp-Type-Class
C#
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Extension Method in C#
HashSet in C# with Examples
Top 50 C# Interview Questions & Answers
C# | How to insert an element in an Array?
C# | Inheritance
Partial Classes in C#
C# | List Class
Lambda Expressions in C#
Difference between Hashtable and Dictionary in C#
Convert String to Character Array in C#