C# Keywords:
C# contains reserved words, that have special meaning for the compiler. These reserved words are called "keywords". Keywords cannot be used as a name (identifier) of a variable, class, interface, etc.
Keywords in C# are distributed under the following categories:
Modifier keywords:
Modifier keywords are certain keywords that indicate who can modify types and type members. Modifiers allow or prevent certain parts of programs from being modified by other parts.
Modifier keywords
|
abstract
|
async
|
const
|
event
|
extern
|
new
|
override
|
partial
|
readonly
|
sealed
|
static
|
unsafe
|
virtual
|
volatile
|
Access Modifier Keywords:
Access modifiers are applied on the declaration of the class, method, properties, fields and other members. They define the accessibility of the class and its members.
The default access modifier for class and struct is private and for enum and interface is public.
Access Modifiers
|
Usage
|
public
|
The Public modifier allows any part of the program in the same assembly or another assembly to access the type and its members.
|
private
|
The Private modifier restricts other parts of the program from accessing the type and its members. Only code in the same class or struct can access it.
|
internal
|
The Internal modifier allows other program code in the same assembly to access the type or its members.
|
protected
|
The Protected modifier allows codes in the same class or a class that derives from that class to access the type or its members.
|
Statement Keywords:
Statement keywords are related to program flow.
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
|
Method parameter keywords:
These keywords are applied on the parameters of a method.
Method Parameter Keywords
|
params
|
ref
|
out
|
Namespace keywords:
These keywords are applied with namespace and related operators.
Namespace Keywords
|
using
|
. operator
|
:: operator
|
extern alias
|
Operator Keywords:
Operator keywords perform miscellaneous actions.
Operator Keywords
|
as
|
await
|
is
|
new
|
sizeof
|
typeof
|
stackalloc
|
checked
|
unchecked
|
Access keywords:
Access keywords are used to access the containing class or the base class of an object or class.
Access keywords
|
base
|
this
|
Literal keywords:
Literal keywords apply to the current instance or value of an object.
Literal Keywords
|
null
|
false
|
true
|
value
|
void
|
Type keywords:
Type keywords are used for data types.
Type keywords
|
bool
|
byte
|
char
|
class
|
decimal
|
double
|
enum
|
float
|
int
|
long
|
sbyte
|
short
|
string
|
struct
|
uint
|
ulong
|
ushort
|
Contextual Keywords:
Contextual keywords are considered as keywords, only if used in certain contexts. They are not reserved and so can be used as names or identifiers.
Contextual Keywords
|
add
|
var
|
dynamic
|
global
|
set
|
value
|
Contextual keywords are not converted into blue color (default color for keywords in visual studio) when used as an identifier in Visual Studio. For example, var in the below figure is not in blue color whereas color of this is blue color. So var is a contextual keyword.
Query keywords:
Query keywords are contextual keywords used in LINQ queries.
Query Keywords
|
from
|
where
|
select
|
group
|
into
|
orderby
|
join
|
let
|
in
|
on
|
equals
|
by
|
ascending
|
descending
|
As mentioned above, keyword cannot be used as an identifier (name of variable, class, interface etc). However, they can be used with the prefix '@'. For example, class is a reserved keyword so it cannot be used as an identifier, but @class can be used as shown below.
Example: Keyword as identifier
public class @class
{
public static int MyProperty { get; set; }
}
@class.MyProperty = 100;
Further Reading:
-
Visit MSDN for more information on keywords.
Points to Remember :
- Keywords are reserved words that cannot be used as name or identifier.
- Prefix '@' with keywords if you want to use it as identifier.
- C# includes various categories of keywords e.g. modifier keywords, access modifiers keywords, statement keywords, method param keywords etc.
- Contextual keywords can be used as identifier.