Statements that evaluate to a value are called expressions . You may be surprised how many statements do evaluate to a value. For example, an assignment such as:
myVariable = 57;
is an expression; it evaluates to the value assigned—in this case, 57.
Note that the preceding statement assigns the value 57 to the
variable myVariable
. The assignment
operator (=
) does not test
equality; rather, it causes whatever is on the right side (57) to be
assigned to whatever is on the left side (myVariable
). Chapter 4 discusses some of the
more useful C# operators (including assignment and equality).
Because myVariable = 57
is an
expression that evaluates to 57, it can be used as part of another
assignment, such as:
mySecondVariable = myVariable = 57;
What happens in this statement is that the literal value 57 is
assigned to the variable myVariable
.
The value of that assignment (57) is then assigned to the second
variable, mySecondVariable
. Thus, the
value 57 is assigned to both variables. You can assign a value to any
number of variables with one statement using the assignment operator
(=
), as in the following:
int a,b,c,d,e;a = b = c = d = e = 20;