Skip to content

Implement a generic Enum.Parse method #14074

@rpetrusha

Description

@rpetrusha

Although the Enum.TryParse method (https://msdn.microsoft.com/en-us/library/system.enum.tryparse(v=vs.110).aspx) has two generic overloads, the Enum.Parse overloads (https://msdn.microsoft.com/en-us/library/System.Enum.Parse(v=vs.110).aspx) were added to the .NET Framework in version 1.0, before the introduction of generics. They create the need for rather redundant code, such as

Colors colorValue = (Colors) Enum.Parse(typeof(Colors), colorString);

which entails the need to add an additional cast, as well as instantiate a Type object that represents the enumeration type.

It would be nice to add generic Parse overloads that correspond to the generic TryParse overloads:

public static TEnum Enum.Parse<TEnum>(String value);
public static TEnum Enum.Parse<TEnum>(String value, Boolean ignoreCase);  

Note that there's also another open issue on improving the performance of Enum.TryParse at https://github.com/dotnet/corefx/issues/594.

Metadata

Metadata

Labels

api-needs-workAPI needs work before it is approved, it is NOT ready for implementationarea-System.Runtime

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions