C# keywords
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.
Keywords | Keywords | Keywords | 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 |
void | volatile | while |
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:
Keywords | Keywords |
---|---|
abstract | partial |
async | readonly |
const | sealed |
event | static |
extern | unsafe |
new | virtual |
override | volatile |
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 Modifier | Usage |
---|---|
public | This modifier permits any part of the program, whether it's in the same assembly or another assembly, to access the type and its members. |
private | The 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. |
internal | The 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. |
protected | The 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.
Keywords | Keywords | Keywords |
---|---|---|
if | in | throw |
else | while | try |
switch | break | catch |
case | continue | finally |
do | default | checked |
for | goto | unchecked |
foreach | return | yield |
fixed | lock |
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.
Keywords | Keywords |
---|---|
bool | int |
byte | long |
char | sbyte |
class | short |
decimal | string |
double | struct |
enum | uint |
float | ulong |
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.
Keywords | Keywords |
---|---|
from | join |
where | let |
select | in |
group | on |
into | equals |
orderby | descending |
by | ascending |
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.