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 @.
int value = 1; // fine: "value" is only special inside a setter var var = "var"; // legal, if confusing
Try it
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.
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:
varas a type name would hide the implicit-typing meaning,nameofas a method name changes whatnameof(x)means, and a type namedrecordorasynccauses warnings (lowercase type names are reserved for the language). valueexists as an implicit parameter only insideset,init,addandremoveaccessors. Outside them it is a normal name.async,awaitandyieldare contextual because old code used them as identifiers.awaitis a keyword inside anasyncmethod, lambda or anonymous method (using it as a name there is errorCS4003), and a name likeint awaitalso confuses the parser in expressions such asawait + x, so avoid it.fieldbecame contextual in C# 14, and inside property accessors it now refers to the backing field. See field keyword. A member calledfieldneeds@fieldorthis.fieldthere.extensionis contextual too: it starts an extension block only inside a static class, so older code with a method calledextensionis unaffected.globalis special before::and inglobal using, see global.- You can always force identifier meaning with the
@prefix:@async,@record. The@is not part of the name (@valueandvalueare the same identifier). - Naming your own types and members after contextual keywords is legal but confusing. Prefer different names.