Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.

[Proposal] Enum.IsDefined() #6687

Closed
wants to merge 1 commit into from
Closed

Conversation

benaadams
Copy link
Member

@benaadams benaadams commented Aug 10, 2016

Code for https://github.com/dotnet/corefx/issues/10692

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

{
ulong[] ulValues = Enum.InternalGetValues((RuntimeType)GetType());

return (Array.BinarySearch(ulValues, ToUInt64()) >= 0);
Copy link

@omariom omariom Aug 10, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When it is about 20-30 items in an array it is faster to just iterate over it.

@benaadams
Copy link
Member Author

Closing as proposal, not active PR

@benaadams benaadams closed this Aug 15, 2016
@karelz karelz added enhancement Product code improvement that does NOT require public API changes/additions and removed api addition labels Mar 17, 2017
@benaadams benaadams deleted the enum-isdefined branch March 27, 2018 05:11
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
enhancement Product code improvement that does NOT require public API changes/additions
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants