Home > C# > Keywords in C#

C# Keywords

In C# Keywords are predefined words with special meaning to the compiler. These are reserved identifiers used in C# for specific purposes. They cannot be used as identifiers (name) for a variable, class or interface in a program unless @ is included as the prefix to make them verbatim identifiers.
E.g., ‘switch’ is one of the keywords in c# & we cannot use ‘switch’ as an identifier, but we can use @switch as an identifier.

Categories of Keywords in c#

We can distribute C# Keywords into following categories.

Access 

They are used to access base class of an object or another class. They include:

  • base
  • this

 

Access Modifier 

Another type of keywords in c# are access modifiers. We use these to restrict access to class members and member functions outside class. These include:

  • public
  • private
  • protected
  • internal

 

Contextual 

Some other words are used to add some specific meaning in code. They are called contextual keywords. They are also used as identifiers, including:

  • add
  • alias
  • async
  • await
  • dynamic
  • get
  • global
  • nameof
  • partial (method)
  • remove select
  • set
  • value
  • var
  • when (filter condition)
  • where (generic type constraint)

 

Literal 

Keywords in c# also include literals. They apply to the current instance of an object.

  • null
  • false
  • true
  • value
  • void

 

Method Parameter 

Method parameters are applied to parameters of a function/ method. They include:

  • params
  • ref
  • out

 

Modifier 

This type of keywords in c# indicate who can modify data types and type members. They also prevent a particular portion of code to be changed by other parts.
Here is a list of these:

  • abstract
  • async
  • const
  • event
  • extern
  • new
  • override
  • partial
  • readonly
  • sealed
  • static
  • unsafe
  • virtual
  • volatile

 

Namespace 

These are applied with namespace and related operators.

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

Operator 

These includes:

  • as
  • await
  • is
  • new
  • sizeof
  • typeof
  • stackalloc
  • checked
  • unchecked

 

Query 

Query keywords are contextual and are used in LINQ queries.

  • from
  • where (query clause)
  • select
  • group
  • into
  • orderby
  • join
  • let
  • in
  • on
  • equals
  • by
  • ascending
  • descending

 

Statement 

These are related to program flow are used to control the flow of a program. i.e., they are used to run a particular portion of code at when some condition is met. They include:

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

 

Type 

Another category of keywords in c# is named as type. They are used for data types. i.e., to define or specify the type for an identifier that it will hold which type of data.

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

 

Example Program

C# Keywords example

Output
Keywords in c# example

As pointed in the image above we used a reserved keyword as an identifier. And in the picture below we accessed its member output is shown.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.