- Displaying Basic Information
- Manipulating Variable Values with Operators
- Understanding Punctuators
- Moving Values with the Assignment Operator
- Working with Mathematical/Arithmetic Operators
- Making Comparisons with Relational Operators
- Understanding Logical Bitwise Operators
- Understanding the Type Operators
- Using the sizeof Operator
- Shortcutting with the Conditional Operator
- Understanding Operator Precedence
- Converting Data Types
- Understanding Operator Promotion
- Bonus Material: For Those Brave Enough
- Summary
- Q&A
- Workshop
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.