Lesson 3.5

C# Assignment Operator – With Programming Examples


Objective

What is Assignment operator in C#?
What is the symbol of Assignment Operator?
How to use Assignment operator in program?

What is Assignment Operators?

An Assignment Operator assigns new value to variable, property or event. It generally used as suffix with arithmetic operator. For example, +=, -= etc. It assigns value to the right hand side operand given by left hand operand.

Assignment Operator Symbol

OperatorDescriptionExample
=It directly assign value to left hand operandnum1 = 20;
+=It add right hand operand to left hand. It is equivalent to num1 = num1 + 5num1 += 5;
// Now num1 value is 25
-=It substract left hand operand based on right hand operand. It is equivalent to num1 = num1 - 5num1 -= 5;
// Now num1 value is 15
*=It multiply left hand operand to right hand operand. It is equivalent to num1 = num1 * 5num1 *= 5;
// Now num1 value is 100
%=It is equivalent to num1 = num1 % 5num1 /= 5;
// Now num1 value is 0

Programming Example

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace assignment_operator
{
    class Program
    {
        static void Main(string[] args)
        {
            int num1 = 20;

            num1 += 5;
            Console.WriteLine(num1);

            num1 -= 5;
            Console.WriteLine(num1);

            num1 *= 5;
            Console.WriteLine(num1);

            num1 %= 5;
            Console.WriteLine(num1);

            Console.ReadKey();
        }
    }
}

		 

Output

25
20
100
0
__

Summary

In this chapter you have learned about assignment operator in C#. In the next chapter you will get some programming exercises. You must clear the programming exercises before looking for next chapter..

BackNext