xxxxxxxxxx
Please thank, like, follow and improve!!!
# Assignment Operators
x = 7: assigning the value of 7 to the variable x
# In-place assignment operators
x += y: Used to represent x = x + y
x -= y: Used to represent x = x - y
x *= y: Used to represent x = x * y
x /= y: Used to represent x = x / y
x %= y: Used to represent x = x % y
xxxxxxxxxx
An assignment operator is the operator used to assign a new value to a variable, property, event or indexer element in C# programming language. Assignment operators can also be used for logical operations such as bitwise logical operations or operations on integral operands and Boolean operands.
Unlike in C++, assignment operators in C# cannot be overloaded directly, but the user-defined types can overload the operators like +, -, /, etc. This allows the assignment operator to be used with those types.
The following are the characteristics of assignment operators:
When using the "=" operator for an assignment with the left operand as the property or indexer access, the property or indexer must have a set accessor.
Overloading a binary operator implicitly overloads its corresponding assignment operator (if any).
The different assignment operators are based on the type of operation performed between two operands such as addition (+=), subtraction, (-=), etc. The meaning of the operator symbol used depends on the type of the operands.
Assignment operators are right-associative, which means they are grouped from right to left.
Although assignment using assignment operator (a += b) achieves the same result as that without ( =a +b), the difference between the two ways is that unlike in the latter example, "a" is evaluated only once.
The assignment operator usually returns a reference to the object so as to be used in multiple assignments made in a single statement such as "a=b=c", where a, b and c are operands.
The assignment operator expects the type of both the left- and right-hand side to be the same for successful assignment.
In C#, an expression using an assignment operator might be "x op y", where x and y are operands and "op" represents the operator. The simple assignment operator "=" is used to store the value of its right-hand operand into the memory location denoted by the left-hand operand. The result is its return value. The other assignment operators that perform indicated operation on the two operands and assign a resulting value to the left operand are called compound assignment operators. These include:
+=
-=
*=
/=
%=
&=
|=
^=
<<= and >>=
Assignment operators do not follow the conventional rule of the first character preference. Rather a method which is followed by an equal sign will have the same precedence as the assignment operator =. The exception to this rule is comparison operators such as <= and != which will follow the first character rule as discussed above.
A Good Practice
While operator precedence is a valid and supported part of Scala, it is still better to show precedence using parenthesis (). This avoids any and all confusion that a programmer reading your code might have.
And on this note, our discussion on operators comes to an end. In the next lesson, you will be challenged to use your knowledge on operator precedence.
xxxxxxxxxx
#include <iostream>
using namespace std;
int main() {
int a, b;
// 2 is assigned to a
a = 2;
// 7 is assigned to b
b = 7;
cout << "a = " << a << endl;
cout << "b = " << b << endl;
cout << "\nAfter a += b;" << endl;
// assigning the sum of a and b to a
a += b; // a = a +b
cout << "a = " << a << endl;
return 0;
}