Assignment Operators


Assignment Operatorsr

The assignment operators are used to assign a value to a variable. Before an arithmetic operator, they are usually employed.

Let's have a look at what C# has to offer in terms of Assignment Operators:

I Identical to ("="): It's one of the most straightforward assignment operators. It assigns one operand's value to another. i.e. the left side operand's value to the right side operand's value.

Example: = a + b

To the Assignment Operator, add Equal: It's a mix of plus "+" and equal to "=", as the name implies. It is written as "+=" and adds the right operand to the left operand before storing the result in the left operand.

For instance, a +=b denotes (a = a+b).

Subtract Equal Assignment Operator: Similar to the add equals operator, it subtracts the value of the right operand from the left operand and then assigns the value to the left operand.

For instance, a -=b denotes (a-b) a = a-b) a-b) a-b)

Division It divides the value of the right operand by the value of the left operand and then saves the result in the left operand, similar to the Assignment Operator.

For instance, a /= b denotes (a= a/b).

Multiply It's the same as the Assignment Operator in that it multiplies the right operand's value by the left operand's value and saves the result in the left operand.

For instance, a *= b denotes (a= a*b).

(It finds the modulus of the left and right operands and stores the value in the left operand. Modulus Equals to the Assignment Operator: It finds the modulus of the left and right operands and stores the value in the left operand.

Example:

a percent =b denotes that (a=a percent b).

The following is a programme to help you gain more clarity:

a += b; int a = 10; int b = 5;

/1 Console.WriteLine(a); a -= b; /2 Console.WriteLine(a); a /= b; /3 Console.WriteLine(a); a *= b; /4 Console.WriteLine(a); a percent = b; /5 Console.WriteLine(a); a percent = b; /6 Console.WriteLine(a); a percent = b;

Output

  • The first number, a = a + b, will return 15.
  • The second operator, a = a-b, will return 10.
  • The third operator, a = a/b, will return 2.
  • The fourth operator, a = a*b, will return 50.
  • The fifth operator, a=a percent b, will yield 0.