Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Proposal] Add Enum.IsDefined() method #18063

Closed
benaadams opened this issue Aug 10, 2016 · 3 comments · Fixed by #33589
Closed

[Proposal] Add Enum.IsDefined() method #18063

benaadams opened this issue Aug 10, 2016 · 3 comments · Fixed by #33589
Labels
api-needs-work API needs work before it is approved, it is NOT ready for implementation area-System.Runtime
Milestone

Comments

@benaadams
Copy link
Member

So rather than doing this:

string DoTheThing(MyEnum argument)
{
    if (!Enum.IsDefined(typeof(MyEnum), argument))
    {
         Log.Warn(...);
         return String.Empty;
    }
    return argument.ToString();
}

You can do this:

string DoTheThing(MyEnum argument)
{
    if (!argument.IsDefined())
    {
         Log.Warn(...);
         return String.Empty;
    }
    return argument.ToString();
}

Enum.IsDefined(typeof(MyEnum), argument) is also quite slow as it needs to do lots of conversion checks etc; whereas if you already have the enum these don't need to be done.

e.g.
https://github.com/dotnet/corefx/blob/master/src/System.Private.Uri/src/System/UriExt.cs#L19-L24

Suggested change dotnet/coreclr#6687

/cc @terrajobst

@benaadams benaadams changed the title Add Enum.IsDefined() method [Proposal] Add Enum.IsDefined() method Aug 10, 2016
@TylerBrinkley
Copy link
Contributor

I apologize for the shameless plug but I believe this is pertinent to users. I've just released version 1.0.0 of Enums.NET, a high-performance type-safe .NET enum utility library.

It has the extension method IsDefined that is constrained to the enum type which addresses this issue.

It also has the extension method IsValid that for flag enums checks if it's a valid combination of the defined flags or for standard enums checks if it's defined.

Enums.NET is available on NuGet and is compatible with .NET Standard 1.0+ as well as .NET Framework 2.0+.

@AlexGhiondea
Copy link
Contributor

This is something we should consider.

The next step would be to have a formal API proposal written and have it reviewed.

@TylerBrinkley
Copy link
Contributor

I've created a formal API proposal dotnet/corefx#15453 that addresses this request.

@msftgits msftgits transferred this issue from dotnet/corefx Jan 31, 2020
@msftgits msftgits added this to the 5.0 milestone Jan 31, 2020
@maryamariyan maryamariyan added the untriaged New issue has not been triaged by the area owner label Feb 23, 2020
@joperezr joperezr modified the milestones: 5.0.0, Future Jul 6, 2020
@joperezr joperezr removed the untriaged New issue has not been triaged by the area owner label Jul 6, 2020
@ghost ghost locked as resolved and limited conversation to collaborators Dec 29, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
api-needs-work API needs work before it is approved, it is NOT ready for implementation area-System.Runtime
Projects
None yet
Development

Successfully merging a pull request may close this issue.

7 participants