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
using System;
using System.Globalization;
using System.Reflection;
class GFG {
enum ABC { A,
B,
C,
D,
E,
F }
public static void Main()
{
try {
ABC a = ABC.A;
Type type = a.GetType();
Array obj = type.GetEnumValues();
Console.Write( "Values of the constants is : {0} " , obj);
}
catch (ArgumentException e)
{
Console.WriteLine( "The current type is not an enumeration." );
Console.Write( "Exception Thrown: " );
Console.Write( "{0}" , e.GetType(), e.Message);
}
}
}
|
Output:
Values of the constants is : GFG+ABC[]
Example 2: For ArgumentException
csharp
using System;
using System.Globalization;
using System.Reflection;
class GFG {
enum ABC { A,
B,
C,
D,
E,
F }
public static void Main()
{
try {
ABC a = ABC.A;
Type type = typeof ( int );
Array obj = type.GetEnumValues();
Console.Write( "Values of the constants is : {0} " , obj);
}
catch (ArgumentException e)
{
Console.WriteLine( "The current type is not an enumeration." );
Console.Write( "Exception Thrown: " );
Console.Write( "{0}" , e.GetType(), e.Message);
}
}
}
|
Output:
The current type is not an enumeration.
Exception Thrown: System.ArgumentException
Reference:
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!