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.
They are used to access base class of an object or another class. They include:
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:
Some other words are used to add some specific meaning in code. They are called contextual keywords. They are also used as identifiers, including:
- partial (method)
- remove select
- when (filter condition)
- where (generic type constraint)
Keywords in c# also include literals. They apply to the current instance of an object.
Method parameters are applied to parameters of a function/ method. They include:
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:
These are applied with namespace and related operators.
- extern alias
- :: operator
Query keywords are contextual and are used in LINQ queries.
- where (query clause)
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:
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.
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.