Home > Articles > Programming > C#

  • Print
  • + Share This
This chapter is from the book

Moving Values with the Assignment Operator

It is now time to learn about the specific operators available in C#. The first operator that you need to know about is the basic assignment operator, which is an equals sign (=). You've seen this operator already in a number of the examples in earlier lessons.

The basic assignment operator is used to assign values. For example, to assign the value 142 to the variable x, you type this:

x = 142;

This compiler places the value that is on the right side of the assignment operator in the variable on the left side. Consider the following:

x = y = 123;

This might look a little weird; however, it is legal C# code. The value on the right of the equals sign is evaluated. In this case, the far right is 123, which is placed in the variable y. Then the value of y is placed in the variable x. The end result is that both x and y equal 123.

CAUTION

You cannot do operations on the left side of an assignment operator. For example, you can't do this:

1 + x = y;

Nor can you put literals or constants on the left side of an assignment operator.

  • + Share This
  • 🔖 Save To Your Account