Statements

Execution of a C# program is specified by a series of statements that execute sequentially in the textual order in which they appear. All statements in a procedural-based language such as C# are executed for their effect. The two most basic kinds of statement in C# are the declaration and expression statements. C# also provides flow control statements for selection, looping and jumping. Finally C# provides statements for special purposes, such as locking memory or handling exceptions.

So that multiple statements can be grouped together, zero or more statements may be enclosed in braces ({ and }), to form a statement block. A statement block can be used anywhere a single statement is valid.

Expression Statements

[ variable =]? expression ;

An expression statement evaluates an expression either assigning its result to a variable or generating side-effects, (i.e., invocation, new, ++, or --). An expression statement ends in a semicolon (;). For example:

x = 5 + 6; // assign result
x++; // side effect
y = Math.Min(x, 20); // side effect and assign result
Math.Min (x, y); // discards result, but ok, there is a side effect
x == y; // error, has no side effect, and does not assign result

Declaration Statements

Variable declaration syntax:

type [ variable [ = expression ]?]+ ;

Constant declaration syntax:

const type [ variable = constant - expression ]+ ;

A declaration statement declares a new variable. You can initialize a variable at the time of its declaration by optionally assigning ...

Get C# Essentials now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.