C# : String Extension to Parse Enums

It seems to me that there is too much typing involved in parsing a string as an Enum type. Here’s a quick string extension method to turn it into a simpler one liner:

public static T EnumParse<T>(this string input, bool ignoreCase = true)
	return (T)Enum.Parse(typeof(T), input, ignoreCase);

With this extension, you can now do this:

string s = "MyEnumValue";
MyEnum e = s.EnumParse<MyEnum>();

Seems shorter to me. 🙂

This entry was posted in C#. Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *