private enum Letter { A, B, C }
public void Main()
{
Console.WriteLine(Letter.A.ToString()); // Non-compliant, use nameof
}
Replace Enum ToString() with nameof.
When using the Enum ToString() syntax to convert an enum value to a string, without any specific format, the result will be the same as using the nameof(value) syntax. While the first one is resolved at runtime and requires some work, the latter is resolved at compile time and is much more efficient.
This rule should not be ignored.
private enum Letter { A, B, C }
public void Main()
{
Console.WriteLine(Letter.A.ToString()); // Non-compliant, use nameof
}
private enum Letter { A, B, C }
public void Main()
{
Console.WriteLine(Letter.A.ToString("D")); // Compliant, the string format impacts the result
}
public void Main()
{
Console.WriteLine(nameof(Letter.A)); // Compliant
}