In this chapter you will learn:
- What is C# Assignment Operators?
- How many types of assignment operators in C sharp?
- How to use assignment operator in program?
The C# assignment operator is generally suffix with arithmetic operators. The symbol of c sharp assignment operator is "=" without quotes. The assignment operator widely used with C# programming. Consider a simple example:
result=num1+num2;
In this example, the equal to (=) assignment operator assigns the value of num1 + num2 into result variable.
Various types of C# assignment operators are mentioned below:
Assignment Operators:
Assignment Operators | Usage | Examples |
---|---|---|
= (Equal to) | result=5 | Assign the value 5 for result |
+= (Plus Equal to) | result+=5 | Same as result=result+5 |
-= (Minus Equal to) | result-=5 | Same as result=result-5 |
*= (Multiply Equal to) | result*=5 | Same as result=result*5 |
/= (Divide Equal to) | result/=5 | Same as result=result/5 |
%= (Modulus Equal to) | result%=5 | Same as result=result%5 |
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace assignment_operator { class Program { static void Main(string[] args) { int num1, num2; num1 = 10; num2 = 5; num1 += num2; // same as num1=num1+num2 Console.WriteLine("Add = {0}", num1); num1 -= num2; // same as num1=num1-num2 Console.WriteLine("\n\nSubtraction = {0}", num1); num1 *= num2; // same as num1=num1*num2 Console.WriteLine("\n\nMultiplication={0}", num1); num1 %= num2; // same as num1=num1%num2 Console.WriteLine("\n\nModulus = {0}", num1); Console.ReadLine(); } } }
Output
Add = 15
Subtraction = 10
Multiplication = 50
Modulus = 0
__
Summary
In this chapter, you learned about different types of assignment operators in C#. You also learned how to use these assignment operators in a program. In next chapter, you will learn about Unary Operator in C#.