JetBrains Rider 2025.1 Help

Code inspection: Use 'nameof' expression to reference enum member name

This inspection suggests using the nameof expression instead of calling ToString() on enum members. The nameof operator is preferable in C# when referencing the name of a member at compile time. This ensures better performance, avoids potential problems during runtime, and improves code readability.

In the example below, instead of converting an enum to a string using .ToString(), you can use the nameof operator to explicitly reference the name of the enum member.

class SampleClass { string Foo() { return MyEnum.FIRST.ToString(); } } public enum MyEnum { FIRST, SECOND }
class SampleClass { string Foo() { return nameof(MyEnum.FIRST); } } public enum MyEnum { FIRST, SECOND }

Switching to the nameof operator offers the following benefits:

  • Ensures compile-time safety, as any changes to the enum name will automatically be updated.

  • Eliminates the runtime overhead associated with the ToString() call.

  • Enhances code clarity by explicitly expressing the developer's intent.

Last modified: 26 March 2025