C# Assignment Operators

The C# Assignment operators are associated with arithmetic operators as a prefix, i.e., +=, -=, *=, /=, %=. The following tables show the available list of assignment operators.

C # Assignment OperatorsOperationExample
+=Plus Equal Tox+=15 is x=x+15
-=Minus Equal Tox- =15 is x=x-15
*=Multiply Equal Tox*=16 is x=x+16
%=Modulus Equal Tox%=15 is x=x%15
/=Divide Equal Tox/=16 is x=x/16

C# Assignment Operators Example

Let us see an example code using the C # assignment operators.

using System;
 
class Assignment_Operators
{
     static void Main()
     {
         int x = 15;

         x += 5;
         Console.WriteLine("x += 5 results x = " + x);
 
         x -= 10;
         Console.WriteLine("x -= 10 results x = " + x);
 
         x *= 2;
         Console.WriteLine("x *= 2 results x = " + x);
 
         x /= 3;
         Console.WriteLine("x /= 3 results x = " + x);
 
         x %= 4;
         Console.WriteLine("x %= 4 results x = " + x);
     }
}

OUTPUT

C# Assignment Operators 1

x is an integer on which we have applied assignment operators in the above C# code.

x+=5
x = x+5 = 20;

x-=10
x = x-10 = 20-10 = 10

x*=2
x = x * 2 = 10 * 2 = 20

x/=3
x = x / 3 = 20 / 3 = 6

x%=4
x = x % 4 = 6 % 4 = 2