Syntax and other highlights of C#.
Identifiers
Identifiers – names of classes, methods, variables, etc. They must consist of Unicode characters and begin with a letter or underscore. Identifiers in C# are case sensitive. By convention, parameters, local variables, and private properties and methods are written in camel case (words are written together, without spaces or underscores, each new word except the first capitalized, for example, myVariavle
), all other identifiers are in Pascal case style (same as camel case, only the first word is capitalized, e.g. MyClass
).
Keywords
Keywords are compiler-reserved names that cannot be used as identifiers. Their complete list (76):
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
void
while
If it is necessary to use a keyword as an identifier, this can be done by prefixing the keyword with the @
symbol, for example, @Class
. In this case, the @
symbol will not be part of the identifier and @myVariable
will be equivalent to myVariable
.
There are also contextual keywords that, within a certain context, become keywords and cannot be used as identifiers, but outside their context they can be identifiers even without the @
symbol. These include:
add
ascending
async
await
by
descending
dynamic
equals
from
get
global
group
in
into
join
let
on
orderby
partial
remove
select
set
value
var
where
yield
Literals
Literals are the simplest pieces of data used in program code (for example, in the expression var myVariable = 12;
, “12” is a literal).
Punctuators
Punctuators are used to delimit the structure of a program (for example, { } ;
). Curly braces group multiple statements into a statement block. A semicolon separates non-block statements (a statement can be split across multiple lines).
Operators
Operators transform and combine expressions. Operators are denoted by symbols, for example, . ( ) * + =
. The dot usually separates the fractional part in numeric literals (the decimal point) or is used as a delimiter for namespaces, classes, and their members. Parentheses are used when declaring and calling methods. An equals sign performs an assignment, a double equals sign performs a comparison.
Comments
Comments can be single-line or multi-line. A single line comment begins with a double slash and continues to the end of the line. Multiline starts with /*
and ends with */
.