C# Reference ยท Keywords and Statements
The nameof operator
Reference for the C# nameof operator: get the name of a variable, member or type as a compile-time string that survives renames and refactoring.
nameof(x) produces the name of a variable, parameter, member, type or namespace as a string. The result is a compile-time constant: the compiler writes the text into your program, and nothing is looked up at run time.
Its purpose is to avoid "magic strings". If you write "customer" in an error message and later rename the parameter, the string silently goes stale. If you write nameof(customer), the rename tool updates it, and a typo becomes a compile error.
void Save(string customer) { ArgumentNullException.ThrowIfNull(customer, nameof(customer)); }
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.
Syntax
nameof(expression) // string, a compile-time constant
| Name | Description |
|---|---|
expression |
A simple name, a qualified name (a.b.c), a member access, a type (including a generic type) or a namespace. It is never evaluated. |
Returns: a string holding only the last identifier of the expression, without namespaces, qualifiers or type arguments.
Errors: the expression must name something that exists. nameof(missing) fails with CS0103. You cannot pass a literal, a method call such as Foo() or an arbitrary expression such as a + b.
Usage notes
- The expression inside
nameofis not executed.nameof(list.Count)works even whenlistisnull. - Only the last segment is returned:
nameof(System.Console.WriteLine)is"WriteLine". If you want"Person.Name", build it yourself withnameof(Person) + "." + nameof(Person.Name). - Generic type arguments are dropped:
nameof(List<int>)is"List". Since C# 14 you can also write an unbound generic type,nameof(List<>), which gives the same result. - It is a constant expression, so you can use it in
constfields,switchcase labels and attribute arguments such as[CallerArgumentExpression(nameof(value))]. - Common uses: parameter names in exceptions,
INotifyPropertyChangednotifications, logging, and[Obsolete]messages that point at replacements. - Inside an
asyncor iterator method,nameof(parameter)is still evaluated at compile time. It does not capture the value. nameofis a contextual keyword. If you have a method namednameofin scope, the call binds to the method instead (the operator is only used when no method of that name is found). Avoid the clash.- Prefer
ArgumentNullException.ThrowIfNull(value)in new code: it fills in the parameter name for you throughCallerArgumentExpression.