Книга: C# 2008 Programmer

Assignment Operators

Assignment Operators

You've already seen the use of the assignment operator (=). It assigns the result of the expression on its left to the variable on its right:

string str = "Hello, world!"; //---str is now "Hello, world!"---
int num1 = 5;
int result = num1 * 6; //---result is now 30--- 

You can also assign a value to a variable during declaration time. However, if you are declaring multiple variables on the same line, only the variable that has the equal operator is assigned a value, as shown in the following example:

int num1, num2, num3 = 5; //---num1 and num2 are unassigned; num3 is 5---
int i, j = 5, k; //---i and k are unassigned; j is 5---

You can also use multiple assignment operators on the same line by assigning the value of one variable to two or more variables:

num1 = num2 = num3;
Console.WriteLine(num1); //---5---
Console.WriteLine(num2); //---5---
Console.WriteLine(num3); //---5---

If each variable has a unique value, it has to have its own line:

int num1 = 4
int num2 = 3
int num3 = 5

Self-Assignment Operators

A common task in programming is to change the value of a variable and then reassign it to itself again. For example, you could use the following code to increase the salary of an employee:

double salary = 5000;
salary = salary + 1000; //---salary is now 6000---

Similarly, to decrease the salary, you can use the following:

double salary = 5000;
salary = salary - 1000; //---salary is now 4000---

To halve the salary, you can use the following:

double salary = 5000;
salary = salary / 2; //---salary is now 2500--

To double his pay, you can use the following:

double salary = 5000;
salary = salary * 2; //---salary is now 10000---

All these statements can be rewritten as follows using self-assignment operators:

salary += 1000; //---same as salary = salary + 1000---
salary -= 1000; //---same as salary = salary - 1000 
salary /= 2; //---same as salary = salary / 2---
salary *= 2; //---same as salary = salary * 2---

A self-assignment operator alters its own value before assigning the altered value back to itself. In this example, +=, -=, /=, and *= are all self-assignment operators.

You can also use the modulus self-assignment operator like this:

int num = 5;
num %= 2; //---num is now 1---

Prefix and Postfix Operators

The previous section described the use of the self-assignment operators. For example, to increase the value of a variable by 1, you would write the statement as follows:

int num = 5;
num += 1; //---num is now 6---

In C#, you can use the prefix or postfix operator to increment/decrement the value of a variable by 1. The preceding statement could be rewritten using the prefix operator like this:

++num;

Alternatively, it could also be rewritten using the postfix operator like this:

num++;

To decrement a variable, you can use either the prefix or postfix operator, like this:

--num;
//---or---
num--;

So what is the difference between the prefix and postfix operators? The following example makes it clear:

int num1 = 5;
int num2 = 5;
int result;
result = num1++;
Console.WriteLine(num1); //---6---
Console.WriteLine(result); //---5---
result = ++num2;
Console.WriteLine(num2); //---6---
Console.WriteLine(result); //---6---

As you can see, if you use the postfix operator (num1++), the value of num1 is assigned to result before the value of num1 is incremented by 1. In contrast, the prefix operator (++num2) first increments the value of num2 by 1 and then assigns the new value of num2 (which is now 6) to result.

Here's another example:

int num1 = 5;
int num2 = 5;
int result;
result = num1++ + ++num2;
Console.WriteLine(num1); //---6---
Console.WriteLine(num2); //---6---
Console.WriteLine(result); //---11---

In this case, both num1 and num2 are initially 5. Because a postfix operator is used on num1, its initial value of 5 is used for adding. And because num2 uses the prefix operator, its value is incremented before adding, hence the value 6 is used for adding. This adds up to 11 (5 + 6). After the first statement, both num1 and num2 would have a value of 6.

Оглавление книги


Генерация: 1.014. Запросов К БД/Cache: 3 / 0
поделиться
Вверх Вниз