By 22/07/2015 13 Comments

C# Assignment Or Compound Operators

  • These operators help us to simplify the coding of expressions.

Example:

          // Without using compound operator:
            int x = 3;
            x = x + 3;
            Console.WriteLine(x);
		
           // Using compound operator:
            int y = 3;
            y += 3; 			
            Console.WriteLine(x);


Suppose:

int number = 3; 

OPERATOR DESCRIPTION
+= Addition assignment, this operator performs an addition action first, and then assign a value to variable or operand (number) e.g. number += 3 = 6 it means add 3 value to number first, and then assign to it back.
-= Subtraction assignment, this operator performs a subtract action first, and then assign a value to variable or operand (number) e.g. Number -= 2 = 1 it means subtract 2 value from number first, and then assign to it back.
*= Multiplication assignment, this operator performs a multiplication action first, and then assign a value to variable or operand (number) e.g. number *= 2 = 6 it means multiply 2 value with number first, and then assign to it back.
/= Division assignment, this operator performs a division action first, and then assign a value to variable or operand (number) e.g. number /= 2 = 1 it means divide 2 value with number first, and then assign to it back.
%= Modulus assignment, this operator gets a reminder action by division first, and then assign a value to variable or operand (number) e.g. number %= 2 = 1 it means divide 2 value with number first, and then assign reminder to it back

Example:

using System;
namespace csharpBasic
{
  // Start class definition / declaration.
    class Program
    {
      // Static main method void type declaration.
        static void Main(string[] args)
        {

            // Initialize and declare number variable of an int type with value of 3.
            int number = 3;

            // This line is called an expression which adds 3 to (number) variable.
            number += 3;

            // Print number variable.
            Console.WriteLine("Addition assignment: {0} ", number);

            // Re-initialize number variable of an int type with value of 3.
            // NOTE: This variable is being re-initialized each time in this entire program.
            number = 3;

            // An expression which performs Subtraction assignment which substract 2 from number variable.
            number -= 2;

            Console.WriteLine("Subtraction Assignment: {0} ", number);
            number = 3;

            // An expression which performs Multiplication assignment which multiply 3 with number variable.
            number *= 3;

            Console.WriteLine("Multiplication Assignment: {0} ", number);
            number = 3;

            // An expression which performs Division assignment which divide 2 with number variable.
            number /= 2;

            Console.WriteLine("Division Assignment: {0} ", number);
            number = 3;

            // An expression which performs Modulus assignment which divide 2 with number variable.
            number %= 2;                 

            Console.WriteLine("Modulus Assignment: {0} ", number);
            Console.ReadKey();

        } // End of main method definition.

    } // End of class.
}







← Previous Tutorial


Posted in: C# Basics, C#.NET

Leave your valuable Comment

Have a natural attraction for women cosmetics and replica watches uk clothes, no
more than two for men the most attractive one, is to make their own driving experience, happy and can serve as the facade of the car, another is to highlight the taste edify sentiment rolex replica watch. The replica rolex is undoubtedly the most fashionable accessories, wear a suit to attend the activities, but also get a decent match on the replica watches .