Skip to content

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

@benaadams

Description

@benaadams

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

Metadata

Metadata

Assignees

No one assigned

    Labels

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

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions