🔥 Articles, eBooks, Jobs, Columnist, Forum, Podcasts, Courses 🎓

Benchmark for ENUM using CSharp | ecode10.com


Benchmark for ENUM using CSharp

What is better and faster to do?

image

What is better and faster to do? Get the black color using .ToString() or use nameOf?

public enum Colors
{
    Green,
    Yellow,
    White,
    Black,
    Blue
}

Code 1 - Enum Colors

I need to get the black color, but what is faster and better?

public class ColorsString
{
    public string GetBlackColorToString()
    {
        return Colors.Black.ToString();
    }

    public string GetColorNameOf()
    {
        return nameof(Color.Black);
    }
}

Code 2 - Two methods

Inside the code 2 I created two methods.

1 - The first one returns string and uses the command Colors.Black.ToString().

2 - The second method returns string and uses the command nameof(Color.Black).

The best one is the second method.

image

Image 1 - Result

Image 1 shows what is the best approach, error probability, and allocated memory.

Tell me, which way you are using?





Related articles




Top