DEV Community

Discussion on: Nameof in C#: Understanding the Usage

Collapse
 
ant_f_dev profile image
Anthony Fung

My favourite usage is in Exception logging.

When refactoring, it's far to easy to rename a method but forget to do the same in an error logging message. All of a sudden, the error message becomes meaningless/cryptic as it says an exception occurred in a method that no longer exists.

But with nameof, the message will be updated too when using refactoring tools.

Collapse
 
bytehide profile image
ByteHide

Completely agree with you. Using 'nameof' in Exception logging is indeed useful and prevents the possible confusion that can occur after refactoring. You've mentioned a crucial point about ensuring the integrity of the error messages. It makes the code maintainability much easier, as the error messages stay meaningful no matter how much refactoring is done.

Thanks for your insightful comment.