Home > Articles > Programming > C#

  • Print
  • + Share This
From the author of

From the author of

Enumeration Expressions

The elements of enumeration expressions evaluate the same as their underlying types. In addition to using normal operators, there are additional methods that can be performed with an enum type. An Enum class is used to obtain the majority of functionality shown in this section. Where the Enum class is being used, the capitalized Enum class name prefixes the method call. The examples in this section, refer to the following enum:

enum Weekday { Mon = 1, Tue, Wed, Thu, Fri, Sat = 10, Sun };

For C++ Programmers

C# enums have much more functionality than C++ enums.

As a typed value, the enum must be assigned to a variable of its type. For example, the underlying representation of a Weekday enum may default to an integral value, but it's still a Weekday type. The following line shows the declaration and initialization of an enum variable:

Weekday w = Weekday.Mon;

During a Console.WriteLine() method call, enum values are printed with their names rather than their underlying integral values. Here's an example:

Console.WriteLine("WeekDay: {0}", w); // WeekDay: Mon

The Format() method returns the string representation of an enum value, as shown here:

Console.WriteLine("Format: {0}", w.Format()); // Format: Mon

To go in the opposite direction and convert a string to an enum, use the FromString() method. The arguments that it accepts are the enum type, the string representation of the value to be converted, and a Boolean condition to verify case. The following example uses the typeof() operator to get the enum type. The string to be converted is True, and the method is case sensitive.

Console.WriteLine("FromString: {0}",
  Enum.FromString(typeof(EnumTest.Weekday),
  "Tue", true));             // FromString: Tue

To get the name of an enum variable, use the GetName() method. The following example shows the GetName() method accepting the enum type and an instance of that enum type, and returning its name as a string.

w = EnumTest.Weekday.Wed;
Console.WriteLine("GetName: {0}",
  Enum.GetName(typeof(EnumTest.Weekday), w)); // GetName: Wed

If there is a need to get the string representations of all the members of an enum, use the GetNames() method—plural of the previous method. The following example shows an array being filled with the names. The method call needs only the enum type.

string[] weekDays = new string[7];
weekDays = Enum.GetNames(typeof(EnumTest.Weekday));

Console.WriteLine("Day 1: {0}", weekDays[0]); // Day 1: Mon
Console.WriteLine("Day 2: {0}", weekDays[1]); // Day 2: Tue
Console.WriteLine("Day 3: {0}", weekDays[2]); // Day 3: Wed
Console.WriteLine("Day 4: {0}", weekDays[3]); // Day 4: Thu
Console.WriteLine("Day 5: {0}", weekDays[4]); // Day 5: Fri
Console.WriteLine("Day 6: {0}", weekDays[5]); // Day 6: Sat
Console.WriteLine("Day 7: {0}", weekDays[6]); // Day 7: Sun

A corresponding method to get the values of an enum is the GetValues() method. The following example shows the GetValues() method accepting an enum type and returning an array of objects. Notice that the array is of type object. In C#, all types are also object types. Therefore, any type can be assigned to the object type.

object[] weekDayVals = new object[7];
weekDayVals = Enum.GetValues(typeof(EnumTest.Weekday));

Console.WriteLine("Day 1: {0}", weekDayVals[0]); // Day 1: Mon
Console.WriteLine("Day 2: {0}", weekDayVals[1]); // Day 2: Tue
Console.WriteLine("Day 3: {0}", weekDayVals[2]); // Day 3: Wed
Console.WriteLine("Day 4: {0}", weekDayVals[3]); // Day 4: Thu
Console.WriteLine("Day 5: {0}", weekDayVals[4]); // Day 5: Fri
Console.WriteLine("Day 6: {0}", weekDayVals[5]); // Day 6: Sat
Console.WriteLine("Day 7: {0}", weekDayVals[6]); // Day 7: Sun

To find out the underlying type of an enum, use the GetUnderlyingType() method. It accepts an enum type argument, and the return value is the integral type of the enum's underlying type. Here's an example:

Console.WriteLine("Underlying Type: {0}", 
  Enum.GetUnderlyingType(
    typeof(EnumTest.Weekday))); // Underlying Type: Int32

When it's necessary to determine whether an enum value is defined, use the IsDefined() method. It accepts an enum type and an enum value, and returns a Boolean true if the value is defined in the enum. Otherwise, it returns false. Here's an example:

w = EnumTest.Weekday.Thu;
Console.WriteLine("Thu is Defined: {0}",
  Enum.IsDefined(typeof(EnumTest.Weekday), w)); 
  // Thu is Defined: True

To obtain an enum type that is set to a specific value, use the ToObject() method. The following example shows the method accepting an enum type and an integer, returning an enum of the requested type with the value corresponding to the integer.

Console.WriteLine("Get Friday: {0}",
  Enum.ToObject(typeof(EnumTest.Weekday), 5)); 
  // Get Friday: Fri 
  • + Share This
  • 🔖 Save To Your Account