Types of Keywords in C#

C# contain reserved words that have special meaning for the compiler. the words which are reserved by C# are known as C# Keywords. These words can not be used as the name of the variable, name of the interface, name of the class, etc.

C# keywords or distributed in the following categories.

  • Modifiers
  • Access modifiers
  • Statement
  • Method Parameter
  • Operator
  • Namespace
  • Access
  • Literal
  • Type
  • Query
  • Contextual

Modifiers Keywords

Modifier keywords are those keywords that indicate modify the type and type members. These keywords allow are to prevent certain parts of a program from being modified by another part below are modifiers keyword.

Modifiers Keywords Description
abstract If a class marked with abstract it's mean this class can only be intended to be a base class of others
async The async keyword indicates that the method is asynchronous
const The keyword indicates that the value of the variable can not be modified
event This keyword used for the declaration of an event
extern For the external implementation of the method
new This keyword explicitly hides the member
override This keyword provides the new implementation of the virtual member
partial This is used for the declaration of the partial class
readonly This keyword indicates that the field can only be assigned as a part of the declaration
sealed If a class marked with sealed it means that class can not be inherited
static A member that itself belongs to a type
unsafe For the declaration of unsafe context
virtual Indicate that the implementation can be changed by an override method
volatile Indicate that a field cannot be modified in a program

Access modifiers

Access modifiers are defined at the stage of the declaration of a class, method, properties, or members. these keywords define the accessibility of the class and its member.

For a class or struct, the default access modifier is private and for the interface and enum is public.

You can find some access modifiers from the below table

Access modifiers Description
Public Access is not limited to a type or type member. This keyword provides complete visibility.
Private Default access modifier type. Access is limited only code in the same class or struct can access it
Protected limited access to the containing type or types derived from the containing class
Internal Limited access to the current assembly only

Statement Keyword

Basically, keywords under this category are program instructions. these keywords are executed in sequence. The below table lists the C# statement keywords.

Statement Keyword
if
else
switch
case
do
for
foreach
in
while
break
continue
default
goto
return
yield
throw
try
catch
finally
checked
unchecked
fixed
lock

statement keywords further divide into the following categories.

Category C# Keywords
Selection statement if,else,switch,case
itteration statement do, for,foreach,while,in
jump statement break, continue, default, goto, return, yield
exception handling throw, try catch, try-finally, try-finally-catch
checked and unchecked checked, unchecked
fixed statement fixed
lock statement lock

Method Parameter

Those keywords are applied as the parameter of the method

Method Parameter
params
ref
out

Namespace Keywords

Those keywords are applied with the namespace. Within a namespace, we can define more of the following types.

  • Using
  • . operator
  • :: operator
  • extern alias

Operator Keyword

C# provides operator keywords a number of them support built-in types and allow us to perform operations. Which are listed below the table.

Operator Keyword Description
as The conversion of an object into the compatible type
await The suspension of asynchronous function to compute the await task
is This keyword checks the type of an object during the run time.
new The creation of a new object, or for the hides an inherited member.
sizeof This keyword uses to obtain the size of a given object.
typeof Returns the system.type of object.
stackalloc The allocation of the block of memory.
checked This keyword is used to explicitly enable overflow checking for integral-type arithmetic operations and conversions
unchecked This keyword is used to suppress overflow-checking for integral-type arithmetic operations and conversions

Access Keywords

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

Access Keywords
base
this

Literal Keywords

Literal keywords apply to the current instance or value of an object.

Literal Keywords
null
false
true
value
void

Type Keywords

Type keywords are used for data types.

Type Keywords
bool
byte
char
class
decimal
double
enum
float
int
long
sbyte
short
string
struct
uint
ulong
ushort

 

Contextual Keywords

Contextual keywords are considered as keywords, only if used in specific contexts. They are not reserved and so can be used as names or identifiers.

These keywords cannot be converted into the default keyword colors which is blue when we use them in c# as an identifier.

Contextual Keywords
add
var
dynamic
global
set
value

Query Keywords

Query keywords are contextual keywords used in query expressions.

Query Keywords Description
from define the range of a variable or data source
where filter the element in the boolean expression.
select return sequence
group a specified group query result according to a specified key-value
into an identifier that can serve as a reference to the results of a join, group or select clause
orderby ascending or descending order of the return result
join for the join of the data source
let the range of the variable
in a keyword in the join clause
on a keyword in the join clause
equals a keyword in the join clause
by a keyword in the group clause
ascending a keyword in an orderby clause
descending a keyword in an orderby clause