Implement Enum.Parse<T>(string value) - by Marek Safar

Status : 

  Fixed<br /><br />
		This item has been fixed in the current or upcoming version of this product.<br /><br />
		A more detailed explanation for the resolution of this particular item may have been provided in the comments section.

Sign in
to vote
ID 328938 Comments
Status Closed Workarounds
Type Suggestion Repros 0
Opened 2/17/2008 10:51:13 AM
Access Restriction Public


I would be nice to add following overload to Enum class

T Parse<T> (T type, string value)


T Parse<T> (string value)

it depends which is better according your guidelines. The first approach has the benefit that existing code would still compile, and you could save a boxing operation.

Sign in to post a comment.
Posted by Microsoft on 7/21/2009 at 4:44 PM
Hello again Marek Safar,

I wanted to inform you that we have added an Enum.TryParse<T> method for the next version of the .NET Framework. It should be available in the next public release. We think that it addresses the concerns you raised here, with the additional benefit of adding TryParse functionality to Enum. I'm going to resolve this issue as fixed. If you disagree with the resolution, feel free to reactive the issue.

Base Class Libraries Team
Posted by Microsoft on 12/8/2008 at 1:00 AM
Hi Marek Safar,

Thanks for the suggestion. I agree that avoiding the boxing here could be useful. We have several suggestions relating to Enum and generics that are worth investigating. We'll look at this issue for future releases and weigh it against our other potential features. I'm going to keep this issue active for tracking purposes, so that we can update it when we have more information and continue to get any comments from the community.

Base Class Libraries Team
Posted by Cabbit on 3/18/2008 at 1:54 PM
I would like to see the T Parse<T>(string value) implementation added. It doesn't need to be anything more then just: return (T)Enum.Parse(typeof(T), value);
would also need a case sensitive version as well. The old Parse implementation shouldn't be removed or changed.

The purpose of high level languages is to make expressing the programmers intent easier. By requiring the typecast it slows the programmer down. Condensing and simplification were the primary design goals of LINQ (you could do everything that LINQ does before LINQ it was just harder and more complicated).
Posted by David A Nelson on 2/19/2008 at 12:23 PM
I don't think your first example accomplishes what you think it does; existing code which uses Enum.Parse could not call it without including the generic type parameter. Having a generic version of Parse is not a bad idea; however, I think the general guidelines the framework team has been using is that if the only benefit of having a generic method is that it saves the end-user from having to do an explicit cast, then it is not worth the maintainability cost and "complexity cost" of implementing it. I think that is the case here.