C# Language nameof Operator


Example

Returns a string that represents the unqualified name of a variable, type, or member.

int counter = 10;
nameof(counter); // Returns "counter"
Client client = new Client();
nameof(client.Address.PostalCode)); // Returns "PostalCode"

The nameof operator was introduced in C# 6.0. It is evaluated at compile-time and the returned string value is inserted inline by the compiler, so it can be used in most cases where the constant string can be used (e.g., the case labels in a switch statement, attributes, etc...). It can be useful in cases like raising & logging exceptions, attributes, MVC Action links, etc...