How to use generic Tryparse with Enum?

You should make a generic method:

private T getEnumStringEnumType<T>() where T : struct, IConvertible
    {
        string userInputString = string.Empty;
        T resultInputType = default(T);
        bool enumParseResult = false;

        while (!enumParseResult)
        {
            userInputString = System.Console.ReadLine();
            enumParseResult = Enum.TryParse<T>(userInputString, out resultInputType);
        }

        return resultInputType;
    }

usage:

public enum myEnum { val1, val2 }

myEnum enumValue = getEnumStringEnumType<myEnum>();

Long ago in Visual Studio 2005 era, I made my own method for TryParse on Enum. I only recently discovered the 2008 implementation and I'm not happy with it's restrictiveness, especially considering that it's a TRY PARSE method; meaning that a programmer is testing an input!

Generally, I prefer to use methods which trust the programmer to know what he's doing :)

My implementation is as follows:

public static bool EnumTryParse<T>(string input, out T theEnum)
{
    foreach (string en in Enum.GetNames(typeof(T)))
    {
        if (en.Equals(input, StringComparison.CurrentCultureIgnoreCase))
        {
            theEnum = (T)Enum.Parse(typeof(T), input, true);
            return true;
        }
    }

    theEnum = default(T);
    return false;
}

The lack of a where T:struct puts the trust in the developer's hands, but it allows you to compile with unknown, generic enums.

As an alternative, you can create a method looping on Enum.GetValues if you want to do an integer comparison when converting to your specified enum.

Hope this helps.


Enum.TryParse is a generic method, which means that its generic type parameters have to be known at compile time. This in turn means that yes, you do have to declare resultInputType as a specific enum type for the code to compile.

If you think on it, the original code is a bit too optimistic: nowhere does it say which enum type should be checked for a member with name equal to userInputString. How could TryParse work without this information?


The TryParse method has the following signature:

TryParse<TEnum>(string value, bool ignoreCase, out TEnum result)
    where TEnum : struct

It has a generic type parameter TEnum that must be a struct and that is used to determine the type of enumeration being parsed. When you don't provide it explicitly (as you did), it will take the type of whatever you provide as the result argument, which in your case is of type Enum (and not the type of the enumeration itself).

Note that Enum is a class (despite it inheriting from ValueType) and therefore it does not satisfy the requirement that TEnum is a struct.

You can solve this by removing the Type parameter and giving the method a generic type parameter with the same constraints (i.e. struct) as the generic type parameter on the TryParse function.

So try this, where I've named the generic type parameter TEnum:

private static TEnum GetEnumStringEnumType<TEnum>()
    where TEnum : struct
{
    string userInputString = string.Empty;
    TEnum resultInputType = default(TEnum);
    bool enumParseResult = false;

    while (!enumParseResult)
    {                
        userInputString = System.Console.ReadLine();
        enumParseResult = Enum.TryParse(userInputString, true, out resultInputType);
    }
    return resultInputType;
}

To call the method, use:

GetEnumStringEnumType<MyEnum>();

Tags:

C#

Enums

Generics