Ads block

Banner 728x90px

c#step4


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.
Access ModifiersUsage
publicThe Public modifier allows any part of the program in the same assembly or another assembly to access the type and its members.
privateThe 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.
internalThe Internal modifier allows other program code in the same assembly to access the type or its members. This is default access modifiers if no modifier is specified.
protectedThe 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.
C# Keywords color in Visual Studio
C# Keywords

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: Use Keyword as Identifier
public class @class
{
    public static int MyProperty { get; set; }
}

@class.MyProperty = 100;
Visit MSDN for more information on keywords.
 Points to Remember :
  1. Keywords are reserved words that cannot be used as name or identifier.
  2. Prefix '@' with keywords if you want to use it as identifier.
  3. C# includes various categories of keywords e.g. modifier keywords, access modifiers keywords, statement keywords, method param keywords etc.
  4. Contextual keywords can be used as identifier.

No comments:

Post a Comment