There's lots of questions on here about converting strings to an enum value. Generally, the answer looks something like the answers on this question:
StatusEnum MyStatus = (StatusEnum) Enum.Parse( typeof(StatusEnum), "Active", true );
While that's a perfectly reasonable answer, and you can write a method to simplify the call, it doesn't answer the question of why Enum.Parse() returns an object
instead of the appropriate enum value. Why do I have to cast it to StatusEnum
?
Edit:
Basically, the question is why is a function like this not part of the Enum class?
public static T Parse<T>(string value) where T: struct
{
return (T)Enum.Parse(typeof (T), value);
}
This function works perfectly fine, does exactly what you'd expect. StatusEnum e = Enum.Parse<StatusEnum>("Active");
.