There are currently 77 keywords defined in the C# language (see Table 2-1). These keywords, combined with the syntax of the operators and separators, form the definition of the C# language. These keywords cannot be used as
|
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 |
int |
interface |
|
internal |
is |
lock |
long |
namespace |
|
new |
null |
object |
operator |
out |
|
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 |
virtual |
volatile |
|
void |
while |
In addition to the keywords, C# defines a set of words that have unique meanings when they are used in certain situations. These are called contextual keywords . They include get , set , yield , value , where , and partial . Because they are not reserved words, outside their context they can be used as names for other program elements, but good practice suggests that it is better not to do so.
In C#, an identifier is a
|
Test |
x |
y2 |
MaxLoad |
|
up |
_top |
my_var |
sample23 |
Remember, you can’t start an identifier with a digit. Thus, 12x is invalid, for example. Good programming practice dictates that you use identifier names that reflect the meaning or usage of the items being named.
Although you cannot use any of the C# keywords as identifier names, C# does allow you to precede a keyword with an @, allowing it to be a legal identifier. For example, @for is a valid identifier. In this case, the identifier is actually for , and the @ is ignored. Here is a program that illustrates the use of an @ identifier:
// Demonstrate an @ identifier. using System; class IdTest { public static void Main() { int @if; // use if as an identifier for(@if = 0; @if < 10; @if++) Console.WriteLine("@if is " + @if); } }
The output shown here proves the @if is properly interpreted as an identifier:
@if is 0 @if is 1 @if is 2 @if is 3 @if is 4 @if is 5 @if is 6 @if is 7 @if is 8 @if is 9
Frankly, using @-qualified keywords for identifiers is not recommended, except for special purposes. Also, the @ can precede any identifier, but this is