Home Dashboard Directory Help
Search

Automatically generate typesafe Parse method for Enum subclasses by Eric Means


Status: 

Closed
 as Fixed Help for as Fixed


3
0
Sign in
to vote
Type: Suggestion
ID: 96897
Opened: 3/23/2005 5:11:42 AM
Access Restriction: Public
Primary Feedback Item: 98356
0
Workaround(s)
view

Description

It would be great if the compiler automatically generated a typesafe Parse method for subclasses of Enum. Currently the user must use the following statement:

Color c = (Color)Enum.Parse(typeof(Color), "Blue");

Which is all kinds of ugly/redundant.
Details
Sign in to post a comment.
Posted by TAG on 3/23/2005 at 6:03 AM
Take a look on suggestion FDBK20833 to create helper methods (class) for every enum.
Or FDBK21375 discussing CLR refusing enum to have methods.

Posted by TAG on 3/23/2005 at 5:31 AM
Agree ;-)
Enums must use generics for this.
But there is no way currently to deduct generic arguments based on return value.
Generics can allow
Color c = Enum.Parse<Color>("Blue");
or
Color x;
bool b = Enum.TryParse("Blue", out x);

Take a look at FDBK14577 for same suggestion
Sign in to post a workaround.