In this article, we’re going to look at how to convert a string and int to an enum in C#. Enums allow us to declare a group of related constant values in a readable way where the underlying data type is usually an integer. 

To download the source code for this article, you can visit our GitHub repository.

Let’s start.

Creating an Example Enum

Let’s first create a simple enum, which we can use for the rest of the article:

public enum DayOfWeek
{
    Monday,
    Tuesday,
    Wednesday,
    Thursday,
    Friday,
    Saturday,
    Sunday
}

Convert a String to an Enum

Suppose we have a string variable equal to "Saturday" and we want to convert it to an enum DayOfWeek object. For this, we can use the Enum.Parse method:

var inputString = "Saturday";
DayOfWeek inputAsEnum = (DayOfWeek)Enum.Parse(typeof(DayOfWeek), inputString);

This will fail though if the casing doesn’t match exactly.

To have it more robust, we can add a true argument at the end so the case is ignored:

var inputString = "SaTurDaY"; 
DayOfWeek inputAsEnum = (DayOfWeek)Enum.Parse(typeof(DayOfWeek), inputString, true);

Convert an Int to an Enum

Converting an integer value to an enum is even more straightforward, by directly casting it to an enum:

var inputInt= 6;
DayOfWeek intAsEnum = (DayOfWeek)inputInt;

As internally the enum starts counting at 0, the value of intAsEnum is Sunday.

Check Whether Int/String is a Valid Enum Value

The two previous examples were chosen to be actually present in theDayOfWeek enum. But, what would happen for instance if we tried to convert the string "Today" or the integer 8?

For this, we can use the Enum.TryParse method (for strings) and the Enum.IsDefined method (for integers). Both methods return a boolean indicating whether it succeeded:

var inputInt = 2;
var inputString = "Tuesday";
var isEnumIntParsed = Enum.IsDefined(typeof(DayOfWeek), inputInt);
var isEnumStringParsed = Enum.TryParse(inputString, true, out DayOfWeek dayOfWeek);

The second argument in Enum.TryParse denotes whether it is case sensitive, while the out argument dayOfWeek is the parsed enum, if parsing succeeds.

The last case to consider is one of the enum flags, which we discussed in an earlier article. Converting an int to an enum having a [Flags] attribute works as usual and gives correct results:

[Flags]
public enum UserType
{
    None = 0,
    Customer = 1,
    Driver = 2,
    Admin = 4,
}

var inputInt = 3;
var parsedEnum = (UserType)inputInt; //parsedEnum equals UserType.Driver | UserType.Customer

However, we can’t just use Enum.IsDefined to check whether it succeeded, as the parsed value might not appear in the enum, but rather can be a collection of multiple values. So, let’s see what is the correct way to check whether it succeeded:

var isEnumIntParsed = Enum.IsDefined(typeof(UserType), inputInt) || parsedEnum.ToString().Contains(",");

This method uses the string representation of the parsedEnum and checks whether there is a comma, which means the conversion was successful and that it is a combination of values. In case it is a single value, for instance when converting the integer 2, the first term in the statement already gives a true value.

Wanna join Code Maze Team, help us produce more awesome .NET/C# content and get paid? >> JOIN US! <<

However, in our example, the inputInt (3) (2+1, i.e. UserType.Driver+ UserType.Customer) is converted to an enum, but because 3 doesn’t appear in UserType the Enum.IsDefined method will return false, hence the extra check for the comma in the parsed string representation.

Conclusion

In this article, we’ve learned how to convert integers and strings representing an enum to an actual enum value. We have also seen how we can check whether it was a valid integer or string, and how even for enum flags we can do this check.