The System.Enum class. Enumeration
This topic is related to the following topic:
Contents
- 1. Class System.Enum. The purpose. Review of methods
- 2. Methods of System.Enum class
- 2.1. Method GetName(). Get the value of a constant as a string. Example
- 2.2. Method GetNames(). Get an array of the names of the enumeration constants. Example
- 2.3. Method GetValues(). Get the array of enumeration constant values. Example
- 2.4. Method Parse(). Convert the string representation of the enum to an object. Example
- 3. An example of using the System.Enum class to determine the number of days in a given month
- Related topics
Search other resources:
1. Class System.Enum. The purpose. Review of methods
The System.Enum class is the base class for enumerations used in a program.
If you declare an enumeration by the sample
enum EnumType { constant1, constant2, ..., constantN };
then a data type named EnumType is created that contains the strictly defined constant names constant1, constant2 …, constantN.
For an instance of type EnumType and for any other instance declared with the enum keyword, the following methods of the base class System.Enum become available (inherited):
- GetValues() – returns an array of constant values in the specified enumeration as an array of the System.Array type;
- GetName() – returns a single value of the specified constant in the specified enumeration as a string;
- GetNames() – returns an array of names of constants of type string[] in the specified enumeration;
- Parse() – converts the string representation of the name or numeric value of one or more enumeration constants into an equivalent enumeration object.
⇑
2. Methods of System.Enum class
2.1. Method GetName(). Get the value of a constant as a string. Example
The GetName() method allows you to get the value of a constant from some enumeration as a string. According to the documentation, the general form of the method is as follows:
public static string GetName(System.Type enumType, object value);
here
- System.Type is an abstract class representing the types of classes, interfaces, arrays, values, enumerations, parameters, generics;
- enumType – enumeration type for which you want to get the name of the value;
- value – the value of a specific constant from the enumType enumeration.
To pass the EnumType enumeration type to the GetName() method, you need to use the typeof() tool, following the pattern
typeof(EnumType)
When using the method, the following types of exceptions are possible:
- System.ArgumentNullException – occurs when the enumType or value is null;
- System.ArgumentException – occurs when the enumType is not an enumeration or value is not an enumType.
Example.
The example below gets the name of the day of the week, which is set in the d variable of the Days type.
using System; namespace ConsoleApp12 { class Program { // An enumeration that defines the days of the week enum Days { Mon, Tue, Wed, Thi, Fri, Sat, Sun }; static void Main(string[] args) { // 1. Declare the instance of enumeration Days d; // 2. Set day of week Tuesday d = Days.Tue; try { // 3. Get the name of the day of the week as a string string strDay = Enum.GetName(typeof(Days), d); // 4. Display the name of the day of the week Console.WriteLine("strDay = {0}", strDay); // strDay = Tue } catch (System.ArgumentNullException e) { Console.WriteLine(e.Message); } catch(System.ArgumentException e) { Console.WriteLine(e.Message); } } } }
⇑
2.2. Method GetNames(). Get an array of the names of the enumeration constants. Example
The GetNames() method allows you to get an array of type string[] containing the names of the specified enumeration constants. According to the documentation, the general form of the method is as follows:
public static string[] GetNames(System.Type enumType);
here enumType is the type of enumeration for which you want to get a string representation of its constants.
When using the method, exceptional situations may arise:
- of type System.ArgumentNullException, if enumType is null;
- of type System.ArgumentException if enumType is not the enumeration of type System.Enum.
Example.
using System; namespace ConsoleApp12 { class Program { // Enumeration describing marks enum Colors { Unsatisfactory = 2, Satisfactory = 3, Good = 4, Excellent = 5 }; static void Main(string[] args) { // 1. Declare a variable of Colors type Colors cl; // 2. Get the names of cl enumeration try { string[] names; names = Enum.GetNames(typeof(Colors)); // 3. Display the names of cl enumeration foreach (string name in names) Console.WriteLine(name); } catch (System.ArgumentNullException e) { Console.WriteLine(e.Message); } catch (System.ArgumentException e) { Console.WriteLine(e.Message); } } } }
The result of the program
Unsatisfactory Satisfactory Good Excellent
⇑
2.3. Method GetValues(). Get the array of enumeration constant values. Example
The value of integer or string constants from an enumeration can be obtained using the GetValues() method, which has the following general form:
public static System.Array GetValues(System.Type enumType);
here
- System.Array – the type of constant values that are returned as a dynamic array;
- System.Type – an abstract class representing the types of classes, interfaces, arrays, values, enumerations, parameters, generics;
- enumType – an instance of the System.Type enumeration.
When using the method, the following types of exceptions may be thrown:
- System.ArgumentNullException – case when enumType == null;
- System.ArgumentException – the case when enumType is not an enumeration;
- System.InvalidOperationException – when the method is called by reflection in a reflection-only context, or the enumType is the type of an assembly that is displayed in a reflection-only context.
Example. The example uses the GetValues() and GetNames() methods to display the names and values of constants from an enumeration that describes the number of days in a particular time of the year.
using System; namespace ConsoleApp12 { class Program { // An enumeration describing the number of days in a season. // It is assumed that there are 28 days in February. enum Seasons { Winter = 90, Spring = 92, Summer = 92, Autumn = 91 }; static void Main(string[] args) { try { // 1. Declare a variable of type Seasons Seasons ss; // 2. Get constant values from ss enum Array values; values = Enum.GetValues(typeof(Seasons)); // 3. Get the names from ss enum string[] names = Enum.GetNames(typeof(Seasons)); // 4. Print the names and values of the enumeration ss for (int i = 0; i < names.Length; i++) Console.WriteLine("{0} = {1}", names[i], (int)values.GetValue(i)); } catch (ArgumentNullException e) { Console.WriteLine(e.Message); } catch (ArgumentException e) { Console.WriteLine(e.Message); } catch (InvalidOperationException e) { Console.WriteLine(e.Message); } } } }
The result of the program
Winter = 90 Autumn = 91 Spring = 92 Summer = 92
⇑
2.4. Method Parse(). Convert the string representation of the enum to an object. Example
The Parse() method allows you to get a single value from an enumeration by its name. The method has several overloaded implementations. According to the documentation, the general form of some method implementations is as follows:
public static object Parse (System.Type enumType, string value) public static object Parse (System.Type enumType, string value, bool ignoreCase)
here
- enumType – variable of type enumeration;
- value – string representation of the enumerated value to be converted;
- ignoreCase – a flag that determines whether to ignore the case of characters (true).
When using the method, the following types of exceptions are possible:
- System.ArgumentNullException – case when enumType = null;
- System.ArgumentException – thrown in cases when enumType is not System.Enum or value is not included in the list of constants of the enumType;
- System.OverflowException – the value is out of range of the enumType.
Example. The example describes a Corners enumeration that contains the number of corners of a known shape. Demonstrates how to determine the number of corners of a Hexagon using the Parse() method. When referring to a shape name in the enumeration, it is not case-sensitive.
using System; namespace ConsoleApp12 { class Program { // Enumeration defining the number of corners in the shape enum Corners { Triangle = 3, Circle = 0, Rectangle = 4, Hexagon = 6, Pentagon = 5 }; static void Main(string[] args) { try { // Get the number of corners of a hexagon, ignore case int nCorners = (int)System.Enum.Parse(typeof(Corners), "HEXAGON", true); Console.WriteLine("nCorners = {0}", nCorners); } catch (ArgumentNullException e) { Console.WriteLine(e.Message); } catch(ArgumentException e) { Console.WriteLine(e.Message); } catch(StackOverflowException e) { Console.WriteLine(e.Message); } } } }
The result of the program
nCorners = 6
⇑
3. An example of using the System.Enum class to determine the number of days in a given month
Task. Develop a program that outputs the number of days in a month based on the number of the month.
Solution. For demonstration purposes, the program uses the capabilities of the System.Enum class to get values from the Months enumeration.
using System; namespace ConsoleApp12 { class Program { // A declaration of the Months enumeration type that describes the months of the year in the class // Jan = 1, Feb = 2, Mar = 3, Apr = 4 , ..., Dec = 12 enum Months { Jan = 1, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov, Dec }; static void Main(string[] args) { // Print the number of days in a month, use a variable of type Enum Console.WriteLine("Enter number of month (1..12):"); int month = Int32.Parse(Console.ReadLine()); // checking if the month is entered correctly if ((month < 1) || (month > 12)) { Console.WriteLine("Wrong input."); return; } Months MN; // variable of type Months // Initialize MN with a value based on the month variable. // The Enum.GetValues() method returns the System.Array class. // The System.Array class has a GetValue() method // that returns the value of the object at index (0..11). MN = (Months)Enum.GetValues(typeof(Months)).GetValue(month - 1); switch (MN) { case Months.Feb: Console.WriteLine("28 (29) days"); break; case Months.Apr: case Months.Jun: case Months.Sep: case Months.Nov: Console.WriteLine("30 days"); break; default: Console.WriteLine("31 days"); break; } } } }
The result of the program
Enter number of month (1..12): 5 31 days
⇑
Related topics
⇑