C# keywords

Learn via video courses
Topics Covered

Overview

C# is a versatile and modern programming language extensively used to create various software applications, including desktop programs, online applications, games, and others. C# provides a collection of keywords that play critical roles in creating the language's syntax, structure, and behaviour. These C# keywords are reserved words that have specific meanings and functionalities within the language.

What are C# Keywords?

Keywords in programming languages like C# are predefined words that have specific meanings and functionalities within the language. They serve as fundamental building blocks for writing code and defining the syntax and behaviour of programs. These C# keywords cannot be used as variable, class, or interface identifier names. If we want to use the keywords as identifiers, we may prefix them with the @ character.

For example, if we consider the following statement:

Here, long is a keyword and val is a variable (identifier). In C#, the keyword long has a special meaning: it denotes the declaration of variables with the "long" data type. This specified role of long cannot be changed.

Also, keywords such as long, int, and char cannot be used as identifiers. So we can't have something like int int;

Complete list of all C# keywords

C# contains many keywords, all of which are written in lowercase. A thorough list of all C# keywords can be found below.

KeywordsKeywordsKeywordsKeywords
abstractasbasebool
breakbytecasecatch
charcheckedclassconst
continuedecimaldefaultdelegate
dodoubleelseenum
eventexplicitexternfalse
finallyfixedfloatfor
foreachgotoifimplicit
inin (generic modifier)intinterface
internalislocklong
namespacenewnullobject
operatoroutout (generic modifier)override
paramsprivateprotectedpublic
readonlyrefreturnsbyte
sealedshortsizeofstackalloc
staticstringstructswitch
thisthrowtruetry
typeofuintulongunchecked
unsafeushortusingusing static
voidvolatilewhile

Keyword Types

C# keywords are classified into the following categories:

Modifier Keywords:

Modifier keywords are specialized C# keywords that specify who can alter types and type members. Modifiers allow or restrict the modification of certain parts of programs by other parts.

List of the modifier keywords:

KeywordsKeywords
abstractpartial
asyncreadonly
constsealed
eventstatic
externunsafe
newvirtual
overridevolatile

Example:

Output:

Access Modifier Keywords:

Access modifiers are utilized when declaring classes, methods, properties, fields, and other elements. They define the level of access allowed to the class and its members.

Access ModifierUsage
publicThis modifier permits any part of the program, whether it's in the same assembly or another assembly, to access the type and its members.
privateThe private modifier restricts access to only the code within the same class or struct, preventing other parts of the program from accessing the type and its members.
internalThe internal modifier allows access by other program codes within the same assembly. It serves as the default access modifier if no modifier is explicitly specified.
protectedThe protected modifier grants access to code within the same class or in a class that inherits from that class, allowing it to access the type or its members.

Example:

Output:

Statement Keywords:

In C#, statement keywords are reserved words that have predefined meanings and are used to create various types of program statements. These C# keywords play a critical role in defining the flow of execution, making decisions, looping, handling exceptions, and more within C# programs.

There are a total of 23 keywords used in program instructions.

KeywordsKeywordsKeywords
ifinthrow
elsewhiletry
switchbreakcatch
casecontinuefinally
dodefaultchecked
forgotounchecked
foreachreturnyield
fixedlock

Example:

Output:

Method Parameter Keywords:

There are three method parameter keywords that change how arguments are processed when supplied to a method.

Keywords
params
ref
out

Example:

Output:

The above example shows the params keyword for variable-length parameter lists, the default value for parameters (b with default 0), and how values are modified within the method.

Namespace Keywords:

This category contains three keywords that are used in namespaces.

Keywords
namespace
using
extern

Example:

Output:

The code defines two namespaces, each with a class having a method. The TestNamespace class calls methods from both namespaces' classes, demonstrating namespace usage and encapsulation.

Operator Keywords:

There are various keywords that are used for different purposes such as creating objects, determining the size of an object, and so on.

Keywords
as
await
is
new
sizeof
typeof
stackalloc
checked
unchecked

Example

Output

Access Keywords:

There are two keywords that are used to access and reference a class or instance of a class.

keywords
base
this

Example

Output

This above C# code defines a hierarchy of classes: Parent and Child. The Parent class has a method for displaying a message. The Child class inherits from Parent and overrides the method. It introduces the usage of base to access the parent's method. In the Main method, an instance of Child is created and its methods are called, demonstrating inheritance and access keywords.

Literal Keywords:

There are various C# keywords which are used as literal or constant.

Keywords
null
false
true
value
void

Example

Output

The above C# code demonstrates the usage of true, false, and null literals.

Type Keywords:

Type keywords in C# are reserved words used to specify data types. These C# keywords are fundamental for declaring variables, parameters, and return types.

KeywordsKeywords
boolint
bytelong
charsbyte
classshort
decimalstring
doublestruct
enumuint
floatulong
ushort

Example

Output

The above C# code demonstrates the usage of type keywords.

Contextual Keywords:

In C#, contextual keywords are words that are treated as keywords only in specific contexts or situations. They can be used as identifiers without causing conflicts, unlike reserved keywords. The compiler recognizes these keywords depending on their context, allowing for more flexible naming within your code.

Keywords
add
var
dynamic
global
set
value

Example

Output

Query Keywords:

In C#, query keywords are used to construct queries using the LINQ (Language Integrated Query) syntax. These C# keywords allow developers to execute operations such as filtering, sorting, grouping, and projecting data from a variety of data sources such as collections, arrays, databases, XML, and others.

KeywordsKeywords
fromjoin
wherelet
selectin
groupon
intoequals
orderbydescending
byascending

Example

Output

In the above example, the from, where, and select keywords are used to construct a query that filters and selects specific numbers from a list.

Conclusion

  • C# provides a collection of keywords that play critical roles in creating the language's syntax, structure, and behavior.
  • C# keywords are reserved words that have specific meanings and functionalities within the language.
  • C# keywords cannot be used as variable, class, or interface identifier names. If we want to use the keywords as identifiers, we may prefix them with the @ character.
  • Modifier keywords are specialized C# keywords that specify who has the ability to alter types and type members.
  • Access modifiers are utilized when declaring classes, methods, properties, fields, and other elements.
  • In C#, statement keywords are reserved words that have predefined meanings and are used to create various types of program statements.