C# Operators


  • Operators allow the processing on the primitive data types and objects.
  • Operators take one or more operand as input and give a result after applying operation over them. Like in "a+b", 'a' and 'b' are the operands and '+' is an operator which performs the add operation on 'a' and 'b' operands.
  • C# contains reach set of operators like arithmetic, relational, logical etc.

In C# Operators can be divided into several categories as given below in the table:


Arithmetic Operators


  • Arithmetic Operators are the simplest operators.
  • These are used for the purpose of mathematical operation like addition, subtraction etc.
  • Here is a list of all arithmetic operators in the given table:
  • Operator Description
    Example

    + Adds two operands A + B = 30
    - Subtracts second operand from the first A - B = -10
    * Multiplies both operands A * B = 200
    / Divides numerator by de-numerator B / A = 2
    % Modulus Operator and remainder of after an integer division B % A = 0
    ++ Increment operator increases integer value by one A++ = 11
    -- Decrement operator decreases integer value by one A-- = 9

    Here is an Example of the Arithmetic Operators please go through that:


    Example

    using System;
    namespace Operators
    {
    class Arithmetic
    {
    static void Main()
    {
    int x = 10;
    int y = 5;
    int z;
    z = x + y;//Addition Operation
    Console.WriteLine("\nAddition of x and y is z={0}",z);
    z = x - y;//Subtraction Operation
    Console.WriteLine("\nSubtraction of x and y is z={0}",z);
    z = x * y;//Multiplication Operation
    Console.WriteLine("\nMultiplication of x and y is z={0}",z);
    z = x / y;//Division Operation
    Console.WriteLine("\nDivision of x and y is z={0}",z);
    z = x % y;//Modulus Operation
    Console.WriteLine("\nModulus of x and y is z={0}",z);
    x++;y++;//Increment Operation
    Console.WriteLine("\nAfter Increment value of x={0} and y={1}", x, y);
    x--; y--;//Decrement Operation
    Console.WriteLine("\nAfter Decrement value of x={0} and y={1}", x, y);
    Console.ReadLine();
    }
    }
    }

    output of the above program is given below:


    Addition of x and y is z=15
    Subtraction of x and y is z=5
    Multiplication of x and y is z=50
    Division of x and y is z=2
    Modulus of x and y is z=0
    After Increment value of x=11 and y=6
    After Decrement value of x=10 and y=5

    Assignment Operators
    • Assignment operators are used to assign values to variables.
    • Here is a list of Assignment operators in the given table:
    • Operator Description Example
      = Simple assignment operator, Assigns values from right side operands to left side operand C = A + B assigns value of A + B into C
      += Add AND assignment operator, It adds right operand to the left operand and assign the result to left operand C += A is equivalent to C = C + A
      -= Subtract AND assignment operator, It subtracts right operand from the left operand and assign the result to left operand C -= A is equivalent to C = C - A
      *= Multiply AND assignment operator, It multiplies right operand with the left operand and assign the result to left operand C *= A is equivalent to C = C * A
      /= Divide AND assignment operator, It divides left operand with the right operand and assign the result to left operand C /= A is equivalent to C = C / A
      %= Modulus AND assignment operator, It takes modulus using two operands and assign the result to left operand C %= A is equivalent to C = C % A
      <<= Left shift AND assignment operator C <<= 2 is same as C = C << 2
      >>= Right shift AND assignment operator C >>= 2 is same as C = C >> 2
      &= Bitwise AND assignment operator C &= 2 is same as C = C & 2
      ^= bitwise exclusive OR and assignment operator C ^= 2 is same as C = C ^ 2
      |= bitwise inclusive OR and assignment operator C |= 2 is same as C = C | 2

    Here is an Example of assignment operators please go through that:


    Example

    using System;
    namespace Operators
    {
    class Assignment
    {
    static void Main()
    {
    int x = 10;
    int y = 5;
    x = y;//simple assignment
    Console.WriteLine("\nResult of x=y:x={0}",x);
    x += y;//addition and assignment
    Console.WriteLine("\nResult of x+=y:x={0}",x);
    x -= y;//subtraction and assignment
    Console.WriteLine("\nResult of x-=y:x={0}",x);
    x*=y;//multiplication and assignment
    Console.WriteLine("\nResult of x*=y:x={0}",x);
    x /= y;//division and assignment
    Console.WriteLine("\nResult of x/=y:x={0}",x);
    x %= y;//modulud and assignment
    Console.WriteLine("\nResult of x%=y:x={0}",x);
    Console.ReadLine();
    }
    }
    }

    Output of the above program is as following:


    Result of x=y:x=5
    Result of x+=y:x=10
    Result of x-=y:x=5
    Result of x*=y:x=25
    Result of x/=y:x=5
    Result of x%=y:x=0

    Bitwise Operator


    • Bitwise operators are used to perform operation on the binary representation of a numeric value.
    • Here is the list of Bitwise operators in the given table:
    • Operator Description Example
      & Binary AND Operator copies a bit to the result if it exists in both operands. (A & B) = 12, which is 0000 1100
      | Binary OR Operator copies a bit if it exists in either operand. (A | B) = 61, which is 0011 1101
      ^ Binary XOR Operator copies the bit if it is set in one operand but not both. (A ^ B) = 49, which is 0011 0001
      ~ Binary Ones Complement Operator is unary and has the effect of 'flipping' bits. (~A ) = 61, which is 1100 0011 in 2's complement due to a signed binary number.
      << Binary Left Shift Operator. The left operands value is moved left by the number of bits specified by the right operand. A << 2 = 240, which is 1111 0000
      >> Binary Right Shift Operator. The left operands value is moved right by the number of bits specified by the right operand. A >> 2 = 15, which is 0000 1111

    Here is an Example of bitwise operators please go through it:


    Example

    using System;
    namespace Operators
    {
    class Bitwise
    {
    static void Main()
    {
    int x = 10;// binary value of 10=1010
    int y = 5;// binary value of 5=0101
    int z;
    z = x & y;//bitwise And operation
    Console.WriteLine("\nResult of x&y:z={0}",z);
    z = x | y;//bitwise Or operation
    Console.WriteLine("\nResult of x|y:z={0}", z);
    z = ~x;//bitwise Not operation
    Console.WriteLine("\nResult of ~x:z={0}", z);
    z = x ^ y;//bitwise Xor operation
    Console.WriteLine("\nResult of x^y:z={0}", z);
    z = x << 1;//bitwise Left shift operation
    Console.WriteLine("\nResult of x<<1:z={0}", z);
    z = x >> 1;//bitwise right shift operation
    Console.WriteLine("\nResult of x>>1:z={0}", z);
    Console.ReadLine();
    }
    }
    }

    output of the above program is as follows:


    Result of x&y:z=0
    Result of x|y:z=15
    Result of ~x:z=11
    Result of x^y:z=15
    Result of x<<1:z=20
    Result of x>>1:z=5

    Logical Operators


    • Logical operators perform operation on Boolean Expression or Boolean data type.
    • Here is the list of Boolean operators in the given table:
    • Operator Description Example
      && Called Logical AND operator. If both the operands are non zero then condition becomes true. (A && B) is false.
      || Called Logical OR Operator. If any of the two operands is non zero then condition becomes true. (A || B) is true.
      ! Called Logical NOT Operator. Use to reverses the logical state of its operand. If a condition is true then Logical NOT operator will make false. !(A && B) is true.
    Here is the truth table for each logical operator:
    • Logical And Operator (&&):
    • Operand1 Operand2 Operand1&&Operand2
      true true true
      true false false
      false true false
      false false false
    • Logical Or Operator (||)
    • Operand1 Operand2 Operand1||Operand2
      true true true
      true false true
      false true true
      false false false
    • Logical Not Operator(!)
    • Operand !Operand
      true false
      false true

    Here is an Example of Logical Operators please goes through it:


    Example

    using System;
    namespace Operators
    {
    class Logical
    {
    static void Main()
    {
    bool b1 = true;
    bool b2 = false;
    bool c;
    c = b1 && b2;//Logical And Operation
    Console.WriteLine("\nResult of b1 && b2:c={0}",c);
    c = b1 || b2;//Logical Or Operation
    Console.WriteLine("\nResult of x||y:c={0}", c);
    c = !b1;//Logical Not Operator
    Console.WriteLine("\nResult of !b1:c={0}", c);
    Console.ReadLine();
    }
    }
    }

    Here is the output of the above program:


    Result of b1&&b2:c=False
    Result of b1||b2:c=True
    Result of !b1:c=False

    Relational Operators


    • Relation operators are used for the comparison between two values.
    • Here is the list of table containing the various relational operators:
    • Operator Description Example
      == Checks if the values of two operands are equal or not, if yes then condition becomes true. (A == B) is not true.
      != Checks if the values of two operands are equal or not, if values are not equal then condition becomes true. (A != B) is true.
      > Checks if the value of left operand is greater than the value of right operand, if yes then condition becomes true. (A > B) is not true.
      < Checks if the value of left operand is less than the value of right operand, if yes then condition becomes true. (A < B) is true.
      >= Checks if the value of left operand is greater than or equal to the value of right operand, if yes then condition becomes true. (A >= B) is not true.
      <= Checks if the value of left operand is less than or equal to the value of right operand, if yes then condition becomes true. (A <= B) is true.

    Here is an Example of the relational operator please go through it:


    Example

    using System;
    namespace Operators
    {
    class Relational
    {
    static void Main()
    {
    int a = 10;
    int b = 12;
    int c = 6;
    Console.WriteLine("\na Console.WriteLine("\na<=b is {0}", (a <= b));
    Console.WriteLine("\na>c is {0}", (a > c));
    Console.WriteLine("\na>=c is {0}", (a >= b));
    Console.WriteLine("\na==b is {0}", (a == b));
    Console.WriteLine("\na!=b is {0}", (a != b));
    Console.ReadLine();
    }
    }
    }

    The output of the above program is given below:


    a a<=b is True
    a>c is True
    a>=c is False
    a==b is False
    a!=b is True

    Other Operators


    In C# there are several other operators which are used for the special task like type, ?, sizeof operator etc listed below in the table:


    Operator Description Example sizeof() Returns the size of a data type. sizeof(int), returns 4. typeof() Returns the type of a class. typeof(StreamReader); & Returns the address of an variable. &a; returns actual address of the variable. * Pointer to a variable. *a; creates pointer named 'a' to a variable. ? : Conditional Expression If Condition is true ? Then value X : Otherwise value Y is Determines whether an object is of a certain type. If( Ford is Car) // checks if Ford is an object of the Car class. as Cast without raising an exception if the cast fails. Object obj = new StringReader("Hello");

    StringReader r = obj as StringReader;

    Here is an Example which is defined the operators mentioned in the above table:


    Example

    using System;
    namespace Operators
    {
    class Relational
    {
    static void Main()
    {
    int a = 10;
    int b = 12;
    int? c = 6;
    Console.WriteLine(a>b?"a>b":"a Console.WriteLine("Size of int data type is {0}",sizeof(int));//sizeof operator
    Console.WriteLine(DateTime.Now);//access operator
    Console.WriteLine(c ?? -1); // '??' operator
    Console.ReadLine();
    }
    }
    }

    output of the above program is given below:


    a Size of int data type is 4
    1/19/2014 4:53:49 PM
    6