C# Language Operators nameof Operator

Help us to keep this website almost Ad Free! It takes only 10 seconds of your time:
> Step 1: Go view our video on YouTube: EF Core Bulk Extensions
> Step 2: And Like the video. BONUS: You can also share it!

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...



Got any C# Language Question?