I finally got fed up with Enum.Parse
July 24th, 2009
I don’t know why I didn’t do this long ago, but I am done writing this:
var val = (SomeEnum)Enum.Parse(typeof(SomeEnum),”someString”);
I have typed this too many times and it annoys me.
I wrote a small extension method on the string type to make this better:
public static class StringExtensions { public static T ToEnum<T>(this string @string) { return (T)Enum.Parse(typeof (T), @string); } }
With this I can now write the previous line as:
var val = "someString".ToEnum<SomeEnum>()

