C# syntax is based on C and C++ syntax. In this section, we describe C#’s elements of syntax, using the following program:
using System; class Test { static void Main( ) { int x = 12 * 30; Console.WriteLine (x); } }
Identifiers are names that programmers choose for their classes, methods, variables, and so on. These are the identifiers in our example program in the order in which they appear:
System Test Main x Console WriteLine
An identifier must be a whole word, essentially made up of Unicode characters starting
with a letter or underscore. C# identifiers are case-sensitive.
By convention, arguments, local variables, and private fields should be in camel
case (e.g., myVariable
), and all other identifiers
should be in Pascal case (e.g., MyMethod
).
Keywords are names reserved by the compiler that you can’t use as identifiers. These are the keywords in our example program:
using class static void int
Here is the full list of C# keywords:
|
|
|
|
---|---|---|---|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| try
|
|
|
|
|
|
|
|
|
|
|
protected |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
internal |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
If you really want to use an identifier that clashes with a keyword, you can qualify it with the @ prefix. For instance:
class class {...} // illegal class @class {...} // legal
The @ symbol doesn’t form part of the identifier itself, so @myVariable
is the same as myVariable
.
Some keywords are contextual, meaning that they can also be used as identifiers—without an @ symbol. The following are contextual keywords:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
With contextual keywords, ambiguity cannot arise within the context in which they are used.
Literals are primitive pieces of data statically embedded into
the program. The literals in our example program are 12
and 30
.
Punctuators help demarcate the structure of the program. These are the punctuators in our example program:
; { }
The semicolon is used to terminate a statement and allows statements to wrap multiple lines:
Console.WriteLine (1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10);
The braces are used to group multiple statements into a statement block.
Operators transform and combine expressions. Most operators in C# are denoted with a symbol, such as the multiplication operator, *. We will discuss operators in more detail later in this book. These are the operators we used in our example program:
. ( ) * =
The period refers to a member of something. The parentheses are used when declaring or calling a method; empty parentheses are used when the method does not accept arguments. The equals sign is used for assignment (the double equals sign, = =, is used for equality comparison).
C# offers two different styles of source code documentation: single-line comments and multiline comments. A single-line comment begins with a double-forward slash and continues until the end of the line. For example:
int x = 3; // comment about assigning 3 to x
A multiline comment begins with /* and ends with */:
int x = 3; /* this is a comment that spans two lines */
Comments may embed XML documentation tags (see the upcoming “XML Documentation” section).