C# Enums with String Values: A Comprehensive Guide
In the realm of C#, enums are a powerful tool for defining a set of named constants. By default, enums in C# are integral types, but did you know you can also use strings as enum values? This feature, introduced in C# 7.1, offers more flexibility and readability, especially when working with human-readable data. Let's dive into the world of C# enums with string values.
Understanding Enums with String Values
Before we delve into the specifics, let's first understand what enums with string values are. In simple terms, they are enums where the underlying type is string. This means you can use string literals as enum values, making your code more readable and maintainable.
Here's a simple example:

public enum DaysOfWeek
{
Monday = "Mo",
Tuesday = "Tu",
Wednesday = "We",
Thursday = "Th",
Friday = "Fr",
Saturday = "Sa",
Sunday = "Su"
}
Benefits of Using Enums with String Values
- Readability: Enums with string values make your code more readable, especially when dealing with human-readable data.
- Flexibility: They offer more flexibility in terms of the data you can represent. You're not limited to integral types.
- Internationalization: They can help with internationalization (i18n) by allowing you to use different strings for different cultures.
Defining and Using Enums with String Values
Defining enums with string values is straightforward. You simply use the = operator to assign a string value to each enum member. Here's an example:
public enum Fruits
{
Apple = "Apple",
Banana = "Banana",
Cherry = "Cherry"
}
To use these enums, you can simply cast them to a string:
string fruit = ((Fruits)1).ToString(); // "Banana"
Underlying Types and Explicit Values
By default, the underlying type of an enum with string values is string. However, you can explicitly specify the underlying type using the underlying keyword. For example:

public enum DaysOfWeek : string
{
Monday = "Mo",
Tuesday = "Tu",
// ...
}
In this case, the underlying type is explicitly string. You can also specify an explicit value for the underlying type, like so:
public enum DaysOfWeek : "Monday"
{
Monday = "Mo",
Tuesday = "Tu",
// ...
}
In this case, the underlying type is string, and the explicit value is "Monday".
Casting and Comparing Enums with String Values
Casting enums with string values to their underlying type is straightforward. For example:

string day = DaysOfWeek.Monday; // "Mo"
int index = (int)DaysOfWeek.Monday; // 0
Comparing enums with string values is also straightforward. You can use the equality operators (==, !=) to compare enum values:
if (DaysOfWeek.Monday == DaysOfWeek.Tuesday)
{
// This will never be true
}
You can also use the Equals method to compare enum values based on their string values:
if (DaysOfWeek.Monday.Equals("Mo"))
{
// This will be true
}
Best Practices
Here are some best practices to keep in mind when working with enums with string values:
- Use them sparingly. While they offer more flexibility, they can also make your code more complex.
- Be consistent with your string values. Use the same casing and formatting throughout your enum.
- Consider using a
Dictionary<string, T>or a similar data structure if you need to map strings to values. Enums with string values can be less performant than other data structures.
That's it! You now know everything you need to know about C# enums with string values. Happy coding!




















