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

# C# Nameof

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

```csharp theme={"dark"}
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.

```csharp theme={"dark"}
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.

```csharp theme={"dark"}
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.

```csharp theme={"dark"}
int @class = 10;
nameof(@class) // Evaluates to: "class"
```

**Generic Type Parameters**
`nameof` can resolve unbound generic type parameters if they are in scope.

```csharp theme={"dark"}
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.

```csharp theme={"dark"}
// 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:

```csharp theme={"dark"}
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
```

<div
  style={{ 
display: "flex", 
justifyContent: "space-between", 
alignItems: "center", 
maxWidth: "754px", 
padding: "1rem 0",
marginBottom: "24px"
}}
>
  <span style={{ fontWeight: "bold", fontSize: "1.25rem", color: "var(--tw-prose-headings)", fontFamily: "Inter, ui-sans-serif, system-ui, sans-serif" }}>Tired of Poor C# Skills? Fix That With Deep Grasping!</span>

  <a
    href="https://syntblaze.com"
    target="_blank"
    style={{ 
  marginLeft: "24px",
  textDecoration: "none", 
  backgroundColor: "#007AFF",
  color: "#ffffff", 
  padding: "6px 16px", 
  borderRadius: "16px",
  fontSize: "0.9rem",
  fontWeight: "600",
  textAlign: "center",
  transition: "background-color 0.2s ease"
}}
  >
    Learn More
  </a>
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/skill-tracking.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=b9b0305c93bb501c9e767b5c76c88835" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/skill-tracking.png" />

  <img src="https://mintcdn.com/syntblazellc/23tyuOzaWS88qFlc/images/nuggets.png?fit=max&auto=format&n=23tyuOzaWS88qFlc&q=85&s=c86c80197299762989e9b882419b2109" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/nuggets.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/bite-sized-exercises.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=a65f9a38c37ff28ab73ed783c53c60e3" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/bite-sized-exercises.png" />
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap", marginTop: "12px" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/mastery-chain.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=748a1763454713e679260fbb95f154a2" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/mastery-chain.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-previews.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=242f61448ff5dd6deaaab2dccc13b507" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-previews.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-explanations.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=cf0fc1c31f9cd0fc26716781be05fbc9" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-explanations.png" />
</div>
