Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

getting error on Enum.GetValues<KnownColor>() how to resolve?

private void GetNames()
{
    Color originColor = Color.FromArgb(255, 206, 206, 206);

    // Simplest, but not necessary the best one
        int Distance(Color left, Color right) =>
          Math.Abs(left.R - right.R) +
          Math.Abs(left.G - right.G) +
          Math.Abs(left.B - right.B) +
          Math.Abs(left.A - right.A);
    
        var result = Enum
          .GetValues<KnownColor>()
          .MinBy(color => Distance(Color.FromKnownColor(color), originColor));
    }

the error is on:

GetValues<KnownColor>

The non-generic method ‘Enum.GetValues(Type)’ cannot be used with type arguments

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

The type argument for Enum.GetValues was added in .NET 5, according to the documentation.

So, either upgrade your .NET version to .NET 5+, or pass it as a method parameter like this:

Enum.GetValues(typeof(KnownColor))

However, unlike the overload that accepts a type parameter, the above returns a generic array rather than an array of KnownColor, so you have to cast it.

var colors = (KnownColor[])Enum.GetValues(typeof(KnownColor))
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading