Skip to content

Enum constrains example - poor code #54021

@RichardD2

Description

@RichardD2

Type of issue

Other (describe below)

Description

The code example under the "Enum Constrains" defines a static extension method:

extension<T>(T) where T : System.Enum
{
    public static Dictionary<int, string> EnumNamedValues() { ... }
}

It then goes on to show an example of using that extension method:

var map = EnumNamedValues<Rainbow>();

This line works within the specific example, but only because the source code has a using static declaration at the top. This declaration is not visible in the article. If someone were to copy the code from the article, and try to call the extension method in this fashion from outside of the class where it was declared, and without an equivalent using static declaration, then it would fail to compile.

The example should be updated to show the extension method being called as an extension method:

var map = Rainbow.EnumNamedValues();

Page URL

https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/generics/constraints-on-type-parameters#enum-constraints

Content source URL

https://github.com/dotnet/docs/blob/main/docs/csharp/programming-guide/generics/constraints-on-type-parameters.md

Document Version Independent Id

22b2c77c-2721-9384-3863-046e391ea4b1

Platform Id

21c05da0-f244-f1c8-dad8-0fb2d0432eca

Article author

@BillWagner

Metadata

  • ID: 5712215d-b167-d9cb-bd51-80df0e300d50
  • PlatformId: 21c05da0-f244-f1c8-dad8-0fb2d0432eca
  • Service: dotnet-csharp
  • Sub-service: fundamentals

Related Issues


Associated WorkItem - 582354

Metadata

Metadata

Assignees

Type

No type
No fields configured for issues without a type.

Projects

Status

🔖 Ready

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions