Skip to main content

Documentation Index

Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt

Use this file to discover all available pages before exploring further.

The 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

nameof(expression)
The 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.
nameof(System.Collections.Generic.List)      // Evaluates to: "List"
nameof(System.Collections.Generic.List<int>) // Evaluates to: "List"
nameof(System.String.Length)                 // Evaluates to: "Length"
Method Identifiers When targeting a method, the operand must be the method group name itself. Including parentheses or arguments results in a compiler error, as that constitutes a method invocation rather than a symbol reference.
nameof(Console.WriteLine)   // Valid: Evaluates to "WriteLine"
// nameof(Console.WriteLine()) // Compiler Error: Cannot use invocation
Verbatim Identifiers If a symbol is defined using a verbatim identifier (prefixed with @ to escape a C# keyword), the nameof operator returns the underlying string name without the @ prefix.
int @class = 10;
nameof(@class) // Evaluates to: "class"
Generic Type Parameters nameof can resolve unbound generic type parameters if they are in scope.
public class Repository<TEntity>
{
    public string GetEntityName()
    {
        return nameof(TEntity); // Evaluates to: "TEntity"
    }
}

Scope and Contextual Rules

  • Local Scope: The operand must be accessible in the context where nameof is invoked. You cannot use nameof on a private member of another class.
  • Instance Members in Static Contexts: You can use nameof on 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 nameof was expanded to allow referencing method parameters within attributes applied to the method itself or its parameters.
// C# 11 feature: Referencing 'message' in the attribute scope
[return: NotNullIfNotNull(nameof(message))]
public string? FormatLog(string? message)
{
    return message;
}

Invalid Operands

The compiler will reject nameof expressions if the operand is not a simple symbol reference. The following are strictly prohibited:
nameof(123)                                  // Error: Numeric literal
nameof("text")                               // Error: String literal
nameof(myArray[0])                           // Error: Indexer access
nameof(x + y)                                // Error: Binary expression
nameof(typeof(int))                          // Error: typeof expression
Master C# with Deep Grasping Methodology!Learn More