TheDocumentation Index
Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt
Use this file to discover all available pages before exploring further.
nameof operator is a compile-time contextual keyword that accepts a code element—such as a variable, type, member, or parameter—as its operand and returns its unqualified string name. Because it is evaluated entirely by the compiler during static analysis, it incurs no runtime execution overhead. The compiler replaces the nameof expression with a constant string literal directly in the emitted Intermediate Language (IL).
Syntax
expression must be a symbol that the compiler can resolve within the current lexical scope. It cannot be a numeric literal, a string literal, or a complex expression requiring runtime evaluation (such as method invocations or mathematical operations).
Evaluation Mechanics
Unqualified Resolution When provided with a fully qualified name or a member access expression,nameof always returns the final, unqualified identifier. It strips away all namespace and declaring type information. When targeting generic types, type arguments may be included in the expression; the compiler simply ignores the type arguments and returns the base identifier.
@ to escape a C# keyword), the nameof operator returns the underlying string name without the @ prefix.
nameof can resolve unbound generic type parameters if they are in scope.
Scope and Contextual Rules
- Local Scope: The operand must be accessible in the context where
nameofis invoked. You cannot usenameofon a private member of another class. - Instance Members in Static Contexts: You can use
nameofon instance members from a static context (e.g.,nameof(MyClass.InstanceProperty)) without requiring an object instantiation. This is permitted because the compiler uses its internal semantic model and symbol table to resolve the identifier’s name during static analysis, rather than evaluating the expression at runtime. - C# 11 Scope Extension: As of C# 11, the scope of
nameofwas expanded to allow referencing method parameters within attributes applied to the method itself or its parameters.
Invalid Operands
The compiler will rejectnameof expressions if the operand is not a simple symbol reference. The following are strictly prohibited:
Master C# with Deep Grasping Methodology!Learn More





