• Javascript
  • Python
  • Go
Tags: c# .net enums

Getting the Underlying Value of an Enum: A Step-by-Step Guide

Enums, or enumerated types, are a powerful feature in many programming languages. They allow developers to define a set of named constants, ...

Enums, or enumerated types, are a powerful feature in many programming languages. They allow developers to define a set of named constants, making code more readable and maintainable. However, sometimes we need to access the underlying value of an enum, which can be a bit more tricky. In this guide, we will go through the steps of getting the underlying value of an enum in a few different scenarios.

Step 1: Understanding Enums

Before we dive into getting the underlying value of an enum, it's important to have a basic understanding of what enums are and how they work. As mentioned earlier, enums are a way to define a set of named constants. These constants are represented by a unique integer value, starting at 0 by default. Enums can also have a specified underlying type, such as int or string.

Step 2: Accessing the Underlying Value

Let's say we have the following enum in our code:

enum Days {Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday}

To access the underlying value of an enum, we can use the static method Enum.GetValues(). This method returns an array of all the named constants in the enum. We can then use the index of the desired constant to access its underlying value.

For example, if we want to get the underlying value of "Thursday" from our Days enum, we would use the following code:

int underlyingValue = (int)Days.Thursday;

This will return the integer value 3, as Thursday is the fourth constant in the enum (starting at 0).

Step 3: Specifying the Underlying Type

In the above example, we used the default underlying type of int. However, we can also specify a different underlying type when defining an enum. Let's say we want our Days enum to have a string underlying type:

enum Days : string {Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday}

To get the underlying value of "Thursday" now, we would use the following code:

string underlyingValue = Days.Thursday.ToString();

This will return the string value "Thursday". It's important to note that when specifying an underlying type, all the constants in the enum must be of the same type.

Step 4: Getting the Underlying Value of a Variable

So far, we have been accessing the underlying value of a named constant in our enum. But what if we have a variable that holds an enum value? How do we get its underlying value?

In this case, we can use the static method Enum.GetName(). This method takes in the type of the enum and the variable, and returns the name of the constant it represents. We can then use this name to get the underlying value using Enum.GetValues().

For example, if we have the following variable:

Days selectedDay = Days.Saturday;

We can get the underlying value of "Saturday" by using the following code:

string underlyingValue = Enum.GetName(typeof(Days), selectedDay);

Step 5: Dealing with Invalid Input

One thing to keep in mind when getting the underlying value of an enum is to handle invalid input. If the input is not a valid constant in the enum, an exception will be thrown. To avoid this, we can use the static method Enum.IsDefined() to check if the given input is a valid constant before trying to get its underlying value.

Conclusion

Enums are a useful tool for defining a set of named constants in our code. And while accessing the underlying value of an enum may seem daunting at first, with the help of the methods provided by the Enum class, it can be done easily. We hope this step-by-step guide has helped you understand how to get the underlying value of an enum in different scenarios. Happy coding!

Related Articles