The
System.Enum class is an abstract class that inherits from
ValueType and implements the
IComparable,
IFormattable, and
IConvertible interfaces. It contains methods to compare instances, convert the value to the string representation, convert the string representation to the numeric representation, and create an instance of a specified enumeration and value.
While this may seem like a simple class, and the methods described below may be well know by some, I have seen many different ways for developers to convert a string representation of an enumeration to the enumeration value. Hopefully this post helps spread the knowledge on the proper ways to do the conversion.
Code
1
using System;
2
3
namespace ALittleClass ...{
4
class Program ...{
5
6
[STAThread]
7
static void Main ( string[] args ) ...{
8
// Formats the enum to a name or decimal equivalent if its not defined
9
// will output 'Sunday'
10
Console.WriteLine ( "Format(G): {0}", Enum.Format ( typeof ( DayOfWeek ), 0, "G" ) );
11
// will output '7'
12
Console.WriteLine ( "Format(G): {0}", Enum.Format ( typeof ( DayOfWeek ), 7, "G" ) );
13
// formats the enum value as a hexadecimal value with out the leading 0x
14
Console.WriteLine ( "Format(X): {0}", Enum.Format ( typeof ( DayOfWeek ), 0, "X" ) );
15
// formats the enum value in decimal form
16
Console.WriteLine ( "Format(D): {0}", Enum.Format ( typeof ( DayOfWeek ), 0, "D" ) );
17
// Behaves identically to "G"
18
Console.WriteLine ( "Format(F): {0}", Enum.Format ( typeof ( DayOfWeek ), 0, "F" ) );
19
20
// gets the name of the enum from the value
21
Console.WriteLine ( "GetName: {0}", Enum.GetName ( typeof ( DayOfWeek ), 1 ) );
22
23
// gets the names of all the values in the enum
24
Console.WriteLine ( "GetNames:" );
25
foreach ( string name in Enum.GetNames ( typeof ( DayOfWeek ) ) ) ...{
26
Console.WriteLine ( "\tName: {0}", name );
27
}
28
29
// gets the underlying type of the enum
30
Console.WriteLine ( "GetUnderlyingType: {0}", Enum.GetUnderlyingType ( typeof ( DayOfWeek ) ).FullName );
31
32
// gets the enum values
33
Console.WriteLine ( "GetValues:" );
34
foreach ( object o in Enum.GetValues ( typeof ( DayOfWeek ) ) ) ...{
35
Console.WriteLine ( "\tValue: {0}", o );
36
}
37
38
// gets if a specified value is defined in the enum. This can be the value of the enum or the name
39
Console.WriteLine ( "IsDefined(Monday): {0}", Enum.IsDefined ( typeof ( DayOfWeek ), "Monday" ) );
40
Console.WriteLine ( "IsDefined(FooBar): {0}", Enum.IsDefined ( typeof ( DayOfWeek ), "FooBar" ) );
41
Console.WriteLine ( "IsDefined(1): {0}", Enum.IsDefined ( typeof ( DayOfWeek ), 1 ) );
42
43
// parse a value and return the enum object
44
DayOfWeek dow = (DayOfWeek)Enum.Parse ( typeof ( DayOfWeek ), "Monday" );
45
Console.WriteLine ( "Parse: {0}", dow );
46
47
// attempting to parse a value with ignore case off.
48
try ...{
49
dow = (DayOfWeek)Enum.Parse ( typeof ( DayOfWeek ), "monday", false );
50
Console.WriteLine ( "Parse: {0}", dow );
51
} catch ( Exception ex ) ...{
52
Console.WriteLine ( ex.Message );
53
}
54
55
// we are out of here!
56
Console.ReadLine ( );
57
}
58
}
59
}
60
Output
Format(G): Sunday
Format(G): 7
Format(X): 00000000
Format(D): 0
Format(F): Sunday
GetName: Monday
GetNames:
Name: Sunday
Name: Monday
Name: Tuesday
Name: Wednesday
Name: Thursday
Name: Friday
Name: Saturday
GetUnderlyingType: System.Int32
GetValues:
Value: Sunday
Value: Monday
Value: Tuesday
Value: Wednesday
Value: Thursday
Value: Friday
Value: Saturday
IsDefined(Monday): True
IsDefined(FooBar): False
IsDefined(1): True
Parse: Monday
Requested value 'monday' was not found.