Add a rule to convert Enum.ToString() to nameof


I think a rule would be useful to replace usages of ToString() on statically known values with equivalent nameof calls.

This means turning this:

Console.WriteLine( SomeEnum.SomeValue.ToString() );

into this:

Console.WriteLine( nameof(SomeEnum.SomeValue) );


nameof replaces the name of the symbol at compile time, while Enum.ToString() is evaluated at runtime. Applying this rule should thus provide some (slight) performance gain.


This is a performance issue and not an error, so I think code smell.

1 Like

I like the reasoning. However, how often does this occur? In most cases, SomeEnum.SomeValue appear in an interpolated string, or in string.Format(). I would argue that $"{nameof(SomeEnum.Value)} etc." is harder to read then $"{SomeEnum.Value} etc.", and in terms of speed, I doubt that you gain anything.

On top of that, there are rules that report on unnecessarily .ToString() calls. So instead of a required SomeEnum.SomeValue.ToString() it might both lead to easier to read code, and gives a small peformance gain, but where .ToString() is not required, I would prefer my colleagues to write SomeEnum.SomeValue and not nameof(SomeEnum.SomeValue).

Just my 2cts.