Skip to content
Tutorials Center
My learning

Topics

C# Reference ยท Keywords and Statements

Contextual keywords

Reference for C# contextual keywords: words such as var, async, record, field and where that are special only in some positions and valid identifiers elsewhere.

A contextual keyword has a special meaning only in a specific place in the grammar. Everywhere else it is an ordinary identifier, so you can use it as a variable, method or class name. This is how C# adds new language features without breaking old programs: async, await, record, required, field and extension could all be added because existing code already using them as names would continue to compile.

The opposite are the reserved keywords such as class or if, which are never identifiers unless you escape them with @.

C#
int value = 1;            // fine: "value" is only special inside a setter
var var = "var";          // legal, if confusing

Try it

C# Contextual keywords used as names

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.

C# The same word, two roles

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.

Contextual keywords by area

Area Keywords
Properties and members add, remove, get, set, init, value, field, required, file, partial
Types and declarations record, global, alias, extension, scoped, unmanaged, notnull, managed, allows
Async and iterators async, await, yield
Generics and constraints where, dynamic, nint, nuint
Patterns and, or, not, when, with, var
Query expressions from, where, select, group, by, into, orderby, ascending, descending, join, on, equals, let
Other args, nameof

Usage notes

  • Some of these are effectively reserved in practice: var as a type name would hide the implicit-typing meaning, nameof as a method name changes what nameof(x) means, and a type named record or async causes warnings (lowercase type names are reserved for the language).
  • value exists as an implicit parameter only inside set, init, add and remove accessors. Outside them it is a normal name.
  • async, await and yield are contextual because old code used them as identifiers. await is a keyword inside an async method, lambda or anonymous method (using it as a name there is error CS4003), and a name like int await also confuses the parser in expressions such as await + x, so avoid it.
  • field became contextual in C# 14, and inside property accessors it now refers to the backing field. See field keyword. A member called field needs @field or this.field there.
  • extension is contextual too: it starts an extension block only inside a static class, so older code with a method called extension is unaffected.
  • global is special before :: and in global using, see global.
  • You can always force identifier meaning with the @ prefix: @async, @record. The @ is not part of the name (@value and value are the same identifier).
  • Naming your own types and members after contextual keywords is legal but confusing. Prefer different names.