Generic Parse method on Enum - a solution

David Findley writes about how he wishes we had a generic Parse method on the Enum class in .NET 2.0.

Though I agree in principle, it's actually quite trivial to create a generic static class with a Parse method, which alleviates some of the pain.

Here's my stab at it:

    public static class EnumUtil<T>
    {
        public static T Parse(string s)
        {
            return (T)Enum.Parse(typeof(T), s);
        }
    }


Say we have the following enum:

    public enum Color
    {
        Black,
        White,
        Blue,
        Red,
        Green
    }

We can now simply use the generic EnumUtil class as follows:

Color c = EnumUtil<Color>.Parse("Black");

I feel that this helper method doesn't actually warrant a class in its own right, so you may want to add it to one of your Util classes (if you happen to have a generic one!) instead.

6 Comments

  • Am I wrong or is it a mistake?:


    Color c = EnumUtil.Parse("Black");

    should be


    Color c = EnumUtil.Parse("Black");

    or?

  • Ofcourse Jens - that's because I started out with an enum called Number, but renamed it in-situ to an enum called Color (as that seemed a better example for an enum) while I was editing my post. Fixed now though.

  • It would probably make more sense to use the type parameter at the method level (i.e. public static T Parse(string s)) instead of the class level.

    Having a type-parameterized utility class may be useful in some particular context, but unless it's instantiated for some purposes (such as to wrap or manage the instances of the original class/enum), I'd vote for keeping the type parameterization at the static method level.

  • I'm actually doing this at the moment. Thats how I came up with the idea of Hey, why don't they just do this on the Enum class so millions of developers won't have to go write util code. :)

    Personally I think MS was very conservative with the use of generics in the 2.0 fwk. Once we get to WinFX errr... .NET FWK 3.0 I think the use of gerics will explode. I just hope they remember to give the enum class some love. :P

  • Jouni - you're right, unless you got a good reason for having a generic util class, it makes more sense just having a static generic method.

    David - nice one, your generic TryParse() function. Not sure whether it'd be better to raise an exception or return a boolean if teh value cannot be parsed but hey...

    Dave - yep, it would make a lot of sense, and in many cases they have not really looked at existing classes in the framework and said:"Now, where would it be useful to add generics into the mix...", even if they'd limited that to the System namespace...

  • public static class GenericEnum
    where T : struct, IComparable, IFormattable, IConvertible
    {
    public static bool TryParse(String value)
    {
    bool parsable = false;
    if (Enum.IsDefined(typeof(T), value))
    {
    parsable = true;
    }
    return parsable;
    }

    public static T Parse(String value, bool ignoreCase)
    {
    return (T)Enum.Parse(typeof(T), value, ignoreCase);
    }

    public static T Parse(String value)
    {
    return Parse(value, false);
    }
    }

Comments have been disabled for this content.