Section 3.7. Expressions


3.7. Expressions

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 assignedin 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;  



Learning C# 2005
Learning C# 2005: Get Started with C# 2.0 and .NET Programming (2nd Edition)
ISBN: 0596102097
EAN: 2147483647
Year: 2004
Pages: 250

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net