Behind the Scenes Look at C#: Operators - Assignment Operators
(Page 6 of 8 )
You have seen the first assignment operator (=) and we have used it all over our code examples to assign a value to a variable. The assignment operators have two arguments, the left argument and the right argument ( for example x = 5;). Note that this statement only puts the value 5 in the x variable, but there is another way to assign values like this one (x = y;). In this case you copy the right value (of the y variable) to the left value (into the x variable).
This is the most basic assignment process. It does makes sense that the left value is a physical memory location, so it's a variable (or indexer) and the right value is the value that will be copied to the left value, so it may be a constant value (5), an expression (x + 4), a variable (x) or even a method call that returns a value. Thus, you can't write a statement such as 15 = 10, you must write it in the form x = 10. The assignment operation proceeds from the right to left because it gets the value on the right and copies it to the variable on the left.
C# has many assignment operators other than the simple = operator. There's the addition assignment operator (+=), the subtraction assignment operator (-=), the multiplication assignment operator (*=), the division assignment operator (/=) and the module assignment operator (%=), plus other bitwise assignment operators, which I will discuss with the bitwise operators. For now I will discuss the above operators. Copy the following code into a .cs file, compile it, then run the application.
using System;
namespace Operators
{
class Class1
{
static void Main(string[] args)
{
int x = 5;
int y = 5;
x = x + y;
Console.WriteLine("y assigned to x and x = {0}",x + y);
x += y;
Console.WriteLine("Using the += operator x = {0}", x);
Console.WriteLine("**********");
x = x - y;
Console.WriteLine("x = x - y and x = {0}",x - y);
x -= y;
Console.WriteLine("Using the -= operator x = {0}", x);
Console.WriteLine("**********");
x = x * y;
Console.WriteLine("x = x * y and x = {0}",x * y);
x *= y;
Console.WriteLine("Using the *= operator x = {0}", x);
Console.WriteLine("**********");
x = x / y;
Console.WriteLine("x = x / y and x = {0}",x / y);
x /= y;
Console.WriteLine("Using the /= operator x = {0}", x);
Console.WriteLine("**********");
x = x % y;
Console.WriteLine("x = x % y and x = {0}",x % y);
x %= y;
Console.WriteLine("Using the %= operator x = {0}", x);
Console.ReadLine();
}
}
}
The result is:

The complex assignment operators don't just copy the right value into the left value; it's a little more complicated than that. For example, the += operator takes the value of the right side and adds it to the left side. It's the same as coding x = x + y, but the C# compiler doesn't process it that way because it knows exactly what the += operator does. To understand how these operators work, I will explain it using x = x operator y expression.
Next: The Ternary Operator >>
More C# Articles
More By Michael Youssef