C# Keywords

In C# there are some predefined keywords which are used while writing a software application. Like to create a class we use class keyword similarly to create an interface we use interface keyword. Reserved keywords have a different meaning, the compiler treats them differently.

A reserved keyword can't be used as an identifier. However by prefixing @ symbol reserved keywords can be used as an identifier. Following is the list of reserved keywords.

Predefined Reserved Keywords

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

Example

int float; //float is a reserved keyword. It can't be used as identifier.
string int = "Hello"; //int is a reserved keyword. It can't be used as identifier.

Contextual Keywords

Contextual keywords are those keywords which are used in special expressions. They give a specific meaning when used in certain statements. Contextual keywords are not reserved keywords, contextual keywords can be used to create identifiers. Following is the list of Contextual Keywords.

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

Example

public int MyProperty 
{ 
    get; //get is a Contextual keywords which is used inside a property 
    set; //set is a Contextual keywords which is used inside a property 
}
 
int set = 0; //Perfectly valid

Let's learn about the flow control in C# in the next section.