In this article, we’re going to learn how to get a string representation of an enum member in C#.

The possibility of defining enumeration types in C# – most widely known by the keyword enum – is a very useful feature. We can use them to assign named constants that represent different possible states of a single type. However, since it’s only possible for us to assign integral numeric values to these constants, there are situations where we prefer the string representation that shows us more clearly what the enum value means.

In these cases, we can use a few options to get the string representation of an enum value.

Support Code Maze on Patreon to get rid of ads and get the best discounts on our products!
Become a patron at Patreon!
To download the source code for this article, you can visit our GitHub repository.

Let’s dive in.

When to Get the Enum Member as a String?

When working with real-life data, it’s not always convenient to have an integer value representing a piece of information. Since an enum outputs an int by default, it might not be the cleanest way to work with them in such cases.

Consider, for instance, a situation in which you must send some information to a database, and one of the fields happens to be represented by an enum in the source code. If you simply save the data as is, the information about that specific field is stored as an int. But there are cases where a number isn’t enough for conveying information.

As an example of that behavior, consider we have an enum that represents one of three different countries:

public enum Country
{
    USA,
    India,
    Australia,
}

After that, we can then create a Person class that uses this enum to set data relating to a person’s country of birth:

public class Person
{
    public string Name { get; set; }
    public Country Country { get; set; }

    public Person(string name, Country country)
    {
        Name = name;
        Country = country;
    }
}

Now, if we try to save an instance of the Person class to any database with just the raw values, the Country field is represented as an int. For instance, an object that has been instantiated as new Person("Bob", Country.USA); will look like this in the database:

IdNameCountry
0"Bob"0

Hence, any person looking at the data won’t be able to know in which country Bob was born. In this case, it would be better to represent Country as a string, making its underlying value clearer for anyone working with that data (in the code).

Additionally, you may need to get the enum value as a string to guarantee consistency between different domains.

For instance, consider that your API must connect to another that uses its own Country enumerable. In this case, even if the countries match, a different order of declaration can scramble the information by mixing up the int values.

The Enum.GetName() Method

As we have seen, knowing how to get an enum member as a string in C# can be essential. Luckily for us, there are a few methods that we can use to do that. The first one we’re going to talk about is the Enum.GetName() method.

This is a static method that has the following signature:

public static string? GetName (Type enumType, object value);

To use the GetName() method, we can pass any declared enum type as the first parameter and any integer as the second one. If the passed enum has a constant that matches the integer, we’ll receive its string representation.

Considering the Country enumerable that we declared earlier, we can get a string value:

var strCountry = Enum.GetName(typeof(Country), 0);
Console.WriteLine(strCountry); // USA

Since the GetName() method’s return type is a nullable string, if we pass it an invalid integer value it returns a null value instead of a string representation.

By exploiting that behavior, we can set up a fallback logic when the value is not present:

var strCountry = Enum.GetName(typeof(Country), 20);
strCountry ??= "Unknown";
Console.WriteLine(strCountry); // Unknown

In this case, we used a null-coalescing assignment to set a default value of “Unknown” if there is no member with the integer value.

As such, by using the Enum.GetName() static method we can easily get the string representation of an enum.

All we need to do is to get its type and the desired member’s underlying integer value.

The Enum.ToString() Method

We already know how to get a string representation of an enum by using its type and an integer value. But in most cases, we work directly with object instances that contain the enum value. In cases like this, it’s easier to get the string value by using the Enum.ToString() method.

To get the string value of Bob’s country that we instantiated in the first section, we only need to call the ToString() method on it:

var bob = new Person("Bob", Country.USA);
var strCountry = bob.Country.ToString();

Console.WriteLine(strCountry); // USA

In comparison, if we wanted to do the same operation using the GetName() method:

var bob = new Person("Bob", Country.USA);
var strCountry = Enum.GetName(typeof(Country), bob.Country);

Console.WriteLine(strCountry); // USA

The difference isn’t that huge, but calling the ToString() method directly is slightly simpler and makes our code more readable.

Even if we don’t have an instance with a set enum value, it’s still possible to use the ToString() method. Since its members are constants, calling the method from one of them directly works the same:

var strCountry = Country.India.ToString();

Console.WriteLine(strCountry); // India

For that reason, while the GetName() method can be used in such cases, but it’s not the best choice usually. As a general rule, you should only use it when working with integer values. In most other cases, the ToString() method is the preferable way of getting an enum member as a string in C#.

The nameof Expression

The final way of getting a string from an enum member in C# is by using the nameof expression.

What sets this method apart is that instead of evaluating the string at runtime, it sets the name of the enum member during compile time. As such, it’s generally faster than the other methods.

But there is a catch. The nameof expression gets the name of a variable, type, or member as a string constant, in that order.

Thus, if we use it on a value that composes an object instance, it doesn’t work:

var bob = new Person("Bob", Country.USA);

var strCountryInstance = nameof(bob.Country);
var strCountryMember = nameof(Country.USA);

Console.WriteLine(strCountryInstance); // Country
Console.WriteLine(strCountryMember); // USA

When we try to get the name of the country from the bob object, the nameof expression gets the name of the Country variable instead. This happens because there are three possible names that the nameof operation could find: Country (variable) → Person (type) → USA (member). Since the nameof expression always tries to get variable and type names before even looking for a member name, we can’t use it in this case.

Considering that, the usage of nameof to get string values of enum members is very specific. When there are so many calls to the ToString() method that it becomes a performance issue, setting the string representation at compile time is a sound choice. But keep in mind that this only works for direct calls to the enum member.

If the enum value is stored in a variable, we must use choose one of the previous methods.

Conclusion

In this article, we’ve learned why and how to get a string representation of an enum member in C#. We went over three different methods of doing it and discussed situations in which to choose one over the other.

Liked it? Take a second to support Code Maze on Patreon and get the ad free reading experience!
Become a patron at Patreon!