Keywords are pre - defined words reserved for the C# compiler, not used as identifiers for these keywords. However, we can prefix the keyword with the @
character if we want to use such keywords as identifiers. In C#, certain identifier in code contexts have a specific meaning, such as get and set contextual keywords. In the following categories, keywords in C# are distributed:
Keywords modifier are certain keywords that show who can modify types and type members. Modifier permits or prevents the modification by other parts of certain program parts.
Modifier keywords |
abstract |
async |
const |
event |
extern |
new |
override |
partial |
readonly |
sealed |
static |
unsafe |
virtual |
volatile |
Access modification is applied to class, method, property, fields and other members declaration. They define the class and its members accessibility.
Access Modifiers | Usage |
public | The Public modifier enables any portion of the program to access the type and its members in the same assembly or other assembly. |
private | The Private modifier limits access to the type and its members for other parts of the program. It can only be accessed through code in the same class or structure. |
internal | The internal modifier allows the access of the type or its members to other program code in the same assembly. This is the default access change when no changes have been specified. |
protected | The Protected modificator allows for the access of codes in the same class or class derived from it. |
Statement Keywords are linked to the flow of programs.
Statement Keywords |
if |
else |
switch |
case |
do |
for |
foreach |
in |
while |
break |
continue |
default |
goto |
return |
yield |
throw |
try |
catch |
finally |
checked |
unchecked |
fixed |
lock |
These keywords are used on a method's parameters.
Method Parameter Keywords |
params |
ref |
out |
These keywords apply to namespace and associated operators.
Namespace Keywords |
using |
. operator |
:: operator |
extern alias |
Operator Keywords perform a variety of actions.
Operator Keywords |
as |
await |
is |
new |
sizeof |
typeof |
stackalloc |
checked |
unchecked |
To access the containing class or base class of an object or class, access
keywords are used.
Access keywords |
this |
base |
For the current instance or value of an object, literal keywords apply.
Literal Keywords |
null |
false |
true |
value |
void |
For data types, Types keywords are used.
Type keywords |
bool |
byte |
char |
class |
decimal |
double |
enum |
float |
int |
long |
sbyte |
short |
string |
struct |
uint |
ulong |
ushort |
Context
keywords are only used in certain context as keywords. They are not reserved, so names or identifiers can be used.
Contextual Keywords |
add |
var |
dynamic |
global |
set |
value |
Note: When used as an identifier in Visual Studio, contextual keywords are not converted into blue (default color in the visual studio keywords).
Keywords used in LINQ queries are contextual keywords.
Query Keywords |
from |
where |
select |
group |
into |
orderby |
join |
let |
in |
on |
equals |
by |
ascending |
descending |
As mentioned previously, a variable name, class, interface etc. may not be used as a keyword.
Example:
using System;
public class @class
{
public static int OurProperty { get; set; }
}
public class Program
{
public static void Main()
{
@class.OurProperty = 100;
Console.WriteLine(@class.OurProperty);
}
}
Output:
For more details on keywords, please visit MSDN.