Section 4.1. The Assignment Operator ()


4.1. The Assignment Operator (=)

The assignment operator causes the operand on the left side of the operator to have its value changed to whatever is on the right side of the operator. The following expression assigns the value 15 to myVariable:

 myVariable = 15; 

The assignment operator also allows you to chain assignments, assigning the same value to multiple variables , as follows :

 myOtherVariable = myVariable = 15; 

The previous statement assigns 15 to myVariable , and then also assigns the value (15) to myOtherVariable . This works because the statement:

 myVariable = 15; 

is an expression; it evaluates to the value assigned. That is, the expression:

 myVariable = 15; 

itself evaluates to 15, and it is this value (15) that is then assigned to myOtherVariable .

It is important not to confuse the assignment operator ( = ) with the equality, or equals, operator ( == ), which has two equals signs and is described later in the chapter. The assignment operator does not test for equality; it assigns a value.




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