How can I use the string value of a C# enum value in a case statement?

Solution 1:

Since C# 6, you can use: case nameof(SomeEnum.SomeValue):

Nameof is evaluated at compile time, simply to a string that matches the (unqualified) name of the given variable, type, or member. Naturally, it changes right along should you ever rename the enum option name.

Solution 2:

Convert the string in your switch to an enum value.

(ORDER)Enum.Parse(typeof(ORDER), value, true);

Solution 3:

The enum is a constant, but the result of .ToString() is not. As far as the compiler is concerned, it is a dynamic value. You probably need to convert your switch case into a series of if/else statements

Solution 4:

As an alternative to using if .. else, you could convert your string to an enum first. It would probably not make much sense if the number of options is small though:

if (Enum.IsDefined(typeof(ORDER), value))
{
    switch ((ORDER)Enum.Parse(typeof(ORDER), value)
    {
        case ORDER.partial01:
            // ... 
            break;
        case ORDER.partial12:
            // etc
    }
}
else
{
    // Handle values not in enum here if needed
}

*sigh* if only there was a built-in T Enum.Parse<T>(string value), and a TryParse version :)