Summary#
nameof(symbol) validates the symbol and folds — at compile time — to its simple-name string
literal: the LAST identifier of the chain, exactly C#. There is no runtime or stored surface: the
persisted node is an ordinary String literal (like const-inlining, the decompiled form shows the folded
string).
Signature#
nameof(<name>) // a local, param, entity set, or module
nameof(<Type>.<Member>) // the type-member form → "Member"
nameof(<value>.<Member>) // the value-member form → "Member"Description#
Valid symbols, each folding to the last identifier:
- a parameter or local —
nameof(count)→"count"; - an entity / type name —
nameof(Order)→"Order"; a namespaced set folds to the simple name (nameof(osy.User)→"User"); - the type-member form —
nameof(Order.Total)→"Total"; - the value-member form —
nameof(o.Total)→"Total"(the chain must resolve); - stdlib modules —
nameof(Math)→"Math".
Invalid symbols are compile errors, as in C#: nameof(missing) →
nameof: unknown symbol 'missing'; nameof(o.Nope) → nameof: cannot resolve 'Nope' …;
nameof(1 + 2) → nameof requires a simple name or member access.
A local variable actually named nameof shadows the operator (C# contextual-keyword behavior).
Typical use: validation and error messages that survive renames.
Examples#
entity Order { decimal Total; }
string Examples(Order o, int count) {
var total = 5;
var a = nameof(count); // "count" — a parameter
var b = nameof(total); // "total" — a local
var c = nameof(Order); // "Order" — an entity name
var d = nameof(Order.Total); // "Total" — the type-member form
var e = nameof(o.Total); // "Total" — the value-member form
return a + b + c + d + e;
}See also#
- const — the other compile-time fold (nameof stores exactly like an inlined const)