C# Basics

C# Keywords

Identifiers and keyword identifiers are the names used to determine classes, functions, variables, or any item defined by the programmer. Basically, C# keywords are predefined, reserved words that have a special status and meaning to the compiler. However, if you have to use a keyword as an identifier, you must prefix it with the @ character to indicate that this should be treated as an identifier, not as a C# keyword.

  • An identifier cannot be a C# keyword.
  • Prefix the C# keyword with the @ symbol to use it as an identifier.
  • C# identifiers are case-sensitive.
  • The compiler uses keywords to identify the structure and organization of the code.
  • 79 predefined, reserved keywords.
  • 25 contextual keywords that have a special meaning in a specific context.
  • 104 actual C# keywords in the language.
  • Visual Studio 2017 shows C# keywords in blue to identify them easily.

readonly is a reserved word known as a keyword but can be used as identifier by adding the @ symbol as a prefix.

int readonly = 10; // invalid
int @readonly = 10; // valid

The above code will create a variable @readonly of type int.

abstract as base bool true try typeof uint
break byte case catch ulong unchecked unsafe ushort
char checked class const using using static virtual void
continue decimal default delegate volatile while    
do double else enum true try typeof uint
event explicit extern false ulong unchecked unsafe ushort
finally fixed float for using using static virtual void  
foreach goto if implicit volatile while    
in int interface internal true try typeof uint
is lock long namespace ulong unchecked unsafe ushort
new null object operator using using static virtual void
out override params private volatile while    
protected public readonly ref true try typeof uint
return sbyte sealed short ulong unchecked unsafe ushort
sizeof stackalloc static string using using static virtual void
struct switch this throw volatile while    

Contextual Keywords in C#

A contextual keyword can be used as a variable, class, or property name, but when it is used at a certain location in the code, it becomes a keyword. While it is not a reserved word in C#, it is best not to use these names as identifiers.

add alias ascending
async await by
descending dynamic equals
from get global
group into join
let nameof on
orderby partial (type) partial (method)
remove select set
value var when (filter condition)
where (generic type constraint) where (query clause) yield

C# Reference | Microsoft Docs