English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

C# Keywords

 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:

Modifiers

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
abstractasyncconsteventexternnewoverridepartialreadonly
sealedstaticunsafevirtualvolatile



Access Modifier Keywords:

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 ModifiersUsage
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

Statement keywords are related to program flow.

Statement keywords
ifelseswitchcasedoforforeachinwhilebreakcontinuedefaultgotoreturn
yieldthrowtrycatchfinallycheckeduncheckedfixedlock




Method parameter keywords

  These keywords are applied to method parameters.

Method parameter keywords
paramsrefout

Namespace keywords

These keywords are applied with namespaces and related operators.

Namespace keywords
using.operator::operatorextern alias

Operator keywords

Operator keywords perform other operations.

Operator keywords
asawaitisnewsizeof
typeofstackalloccheckedunchecked

Access keywords

Access keywords are used to access the containing class or base class of an object or class.

Access keywords
basethis

Literal keywords

Literal keywords are used for the current instance or value of an object.

Literal keywords
nullfalsetruevaluevoid

Type keywords

Type keywords are used for data types.

Input keywords
boolbytecharclassdecimaldoubleenumfloatintlong
sbyteshortstringstructuintulongushort


Content-related keywords

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
addvardynamicglobalsetvalue

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

Query keywords are context keywords used in LINQ queries.

Query keywords
fromwhereselectgroupintoorderbyjoin
letinonequalsbyascendingdescending

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: 

  1. Keywords are reserved words and cannot be used as names or identifiers.

  2. If you want to use keywords as identifiers, please add '@' before the keyword.

  3. C# includes various categories of keywords, such as modifier keywords, access modifier keywords, statement keywords, method parameter keywords, and so on.

  4. Contextual keywords can be used as identifiers.