Codectionary / Developer documentation / C#

Refer to Symbol Names with nameof

Create compiler-checked name strings for diagnostics without evaluating the named expression.

Read the syntax

Create compiler-checked name strings for diagnostics without evaluating the named expression.

nameof(string.Length) yields the member’s simple name, Length.

What it is and why to use it

A parameter name written as a string can become stale after a refactor. nameof produces a compile-time string for a variable, type or member that the compiler can resolve. It is useful in exception parameter names and some notification APIs. It names the symbol rather than reading its runtime value.

How it works

nameof returns the simple name, not a fully qualified path or a property’s value. The operand must refer to a permitted symbol. The expression is not evaluated at runtime. Newer C# versions extend the syntax accepted in some cases; these examples use forms supported by the documented C# 12 setting.

Common mistakes and limitations

Do not expect nameof(person.Name) to return John or to call the property getter. Do not use it as a stable external storage key if renaming the C# member must not change a wire format. Use explicit schema names for those contracts.

Run the examples locally

These complete Program.cs examples use a .NET 10 console project with the C# language version set to 12.0, nullable checking enabled and implicit usings disabled. They use standard .NET libraries and need no extra NuGet packages. This is the verified environment, not a claim that every API requires .NET 10 or works unchanged in .NET Framework, Unity or other runtimes.

Install a supported .NET 10 SDK, create the console project, replace its project file with the settings below, then replace Program.cs with one complete example at a time. Build and run from the project folder. using directives bring namespaces into scope; top-level statements form the program’s entry point, and any helper types follow those statements. Console.WriteLine writes terminal output. Do not combine examples that declare the same type names.

C# in Codectionary’s Playground is editing-only: it does not compile or execute these programs. Run them with the local .NET SDK to reproduce the expected output. Async examples use controlled in-process operations rather than live network services or timing-sensitive delays.

Create and enter a project

dotnet new console -n CSharpExample --framework net10.0
cd CSharpExample

CSharpExample.csproj settings used for these examples

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net10.0</TargetFramework>
    <LangVersion>12.0</LangVersion>
    <Nullable>enable</Nullable>
    <ImplicitUsings>disable</ImplicitUsings>
  </PropertyGroup>
</Project>

Build and run after saving one example in Program.cs

dotnet build
dotnet run --no-build

Continue learning

Change one input and predict the result before running it. Follow the related guides to connect this topic with the surrounding language features.

Syntax

throw new ArgumentOutOfRangeException(nameof(count));

Examples

Distinguish a name from a value

Print a variable name, a member name and the actual stored value.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;

string language = "C#";
Console.WriteLine(nameof(language));
Console.WriteLine(nameof(string.Length));
Console.WriteLine(language);
  1. nameof(language) yields the source-level variable name.
  2. nameof(string.Length) yields the member’s simple name, Length.
  3. Only the final statement reads the runtime value C#. This distinction matters in diagnostic code.
  4. Expected output: language Length C#
  5. Use compiler-checked symbol names in internal diagnostics.

Keep a validation parameter name aligned

Use nameof when reporting a rejected argument.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;

try { Enrol(-1); }
catch (ArgumentOutOfRangeException error) { Console.WriteLine(error.ParamName); }
static void Enrol(int count) {
    if (count < 0) throw new ArgumentOutOfRangeException(nameof(count));
}
  1. The helper rejects a negative count before using it.
  2. nameof supplies the resolved parameter name to the exception constructor.
  3. The catch prints ParamName rather than a locale-dependent complete exception message, making the demonstration output predictable.
  4. Expected output: count
  5. Use compiler-checked symbol names in internal diagnostics.

Best practices

  • Use compiler-checked symbol names in internal diagnostics.
  • Keep externally versioned field names explicit.

Runtime & versions

.NET runtime. Feature availability depends on the language and runtime version used by your project; consult the official reference below.

C# language reference

Specifications & further reading