The C# Assignment operators are associated with arithmetic operators as a prefix, i.e., +=, -=, *=, /=, %=. The following tables show the available list of C# assignment operators.
Symbol | Operation | Example |
---|---|---|
+= | Plus Equal To | x+=15 is x=x+15 |
-= | Minus Equal To | x- =15 is x=x-15 |
*= | Multiply Equal To | x*=16 is x=x+16 |
%= | Modulus Equal To | x%=15 is x=x%15 |
/= | Divide Equal To | x/=16 is x=x/16 |
C# Assignment Operators Example
Let us see an example code using the 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
x is an integer on which we have applied all the available in the above C# code.
x+=5
= x+5 = 20;
x-=10
= x-10 = 20-10 = 10
x*=2
= x*2 = 10 * 2 = 20
x/=3
= x/3 = 20 / 3 = 6
x%=4
= x%4 = 6 % 4 = 2