Home Dashboard Directory Help
Search

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


Status: 

Closed
 as Fixed Help for as Fixed


3
0
Sign in
to vote
Type: Suggestion
ID: 328938
Opened: 2/17/2008 10:51:13 AM
Access Restriction: Public
0
Workaround(s)
view

Description

I would be nice to add following overload to Enum class

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

or

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.

Details
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.

Thanks,
Melitta
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.

Thanks,
Melitta
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.
Sign in to post a workaround.