English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
C# contains reserved words that have special meaning to the compiler. These reserved words are called 'keywords'. Keywords cannot be used as identifiers (variable names, classes, interfaces, etc.).
C# keywords are distributed in the following categories:
Modifier keywords are specific keywords that indicate who can modify types and type members. Modifiers allow or prevent certain parts of the program from being modified by other parts.
Modifier Keywords | ||||||||
---|---|---|---|---|---|---|---|---|
abstract | async | const | event | extern | new | override | partial | readonly |
sealed | static | unsafe | virtual | volatile |
Access modifiers are applied to declarations of classes, methods, properties, fields, and other members. They define the accessibility of the class and its members.
Access Modifiers | Usage |
---|---|
public | The 'public' modifier allows any part of the program within the same assembly or another assembly to access the type and its members. |
private | The 'private' modifier restricts access to the type and its members to other parts of the program. Only code within the same class or structure can access it. |
internal | The 'internal' modifier allows other code within the same assembly to access the type or its members. If no modifier is specified, this is the default access modifier. |
protected | The 'protected' modifier allows code within the same class or derived classes to access the type or its members. |
Statement keywords are related to program flow.
Statement keywords | |||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|
if | else | switch | case | do | for | foreach | in | while | break | continue | default | goto | return |
yield | throw | try | catch | finally | checked | unchecked | fixed | lock |
These keywords are applied to method parameters.
Method parameter keywords | ||
---|---|---|
params | ref | out |
These keywords are applied with namespaces and related operators.
Namespace keywords | |||
---|---|---|---|
using | .operator | ::operator | extern alias |
Operator keywords perform other operations.
Operator keywords | ||||
---|---|---|---|---|
as | await | is | new | sizeof |
typeof | stackalloc | checked | unchecked |
Access keywords are used to access the containing class or base class of an object or class.
Access keywords | |
---|---|
base | this |
Literal keywords are used for the current instance or value of an object.
Literal keywords | ||||
---|---|---|---|---|
null | false | true | value | void |
Type keywords are used for data types.
Input keywords | |||||||||
---|---|---|---|---|---|---|---|---|---|
bool | byte | char | class | decimal | double | enum | float | int | long |
sbyte | short | string | struct | uint | ulong | ushort |
Context keywords are only considered keywords when used in specific contexts. They are not reserved, so they can be used as names or identifiers.
Content-related keywords | |||||
---|---|---|---|---|---|
add | var | dynamic | global | set | value |
When context keywords are used as identifiers in Visual Studio, they will not be converted to blue (the default color of keywords in Visual Studio).
Query keywords are context keywords used in LINQ queries.
Query keywords | ||||||
---|---|---|---|---|---|---|
from | where | select | group | into | orderby | join |
let | in | on | equals | by | ascending | descending |
As mentioned above, keywords cannot be used as identifiers (variable names, classes, interfaces, etc.). However, they can be used with the prefix “@”. For example, the keyword “class” is a reserved keyword, so it cannot be used as an identifier, but it can be used as @class as shown below.
public class @class { public static int MyProperty { get; set; } } @class.MyProperty = 100;
Points to Remember:
Keywords are reserved words and cannot be used as names or identifiers.
If you want to use keywords as identifiers, please add '@' before the keyword.
C# includes various categories of keywords, such as modifier keywords, access modifier keywords, statement keywords, method parameter keywords, and so on.
Contextual keywords can be used as identifiers.