C# Operators

Tutorials PanelPosted by

Operates are symbols that tell the compiler to perform a mathematical or logical operation. There are used to manipulate a variable or constant. And easy form of operator is the “+” operator, which tells to add a value to a variable.

Operators example

                int x = 3;
                x = x + 1;

Where x is an integer and the operator is telling the compiler to add the value ‘1’ to x. The final value of x will be 3.

In C#, the following types of operators are available:

  • Arithmetic operators – Used to perform mathematical operations, such as ‘addition’ and ‘subtraction’.
  • Relational operators – used to check the relationship between two operands.
  • Comparison operators – used to compare two literals or variables.
  • Assignment operators – used to assign values to variables.
  • Logical operators – used with Boolean data type and Boolean expression.
  • Bitwise operators – used to perform operations on binary form of a numerical data.
  • Miscellaneous Operators

Arithmetic Operators

Arithmetic operators are used to perform mathematical operations to variables, such as addition and subtraction.

Example of arithmetic operators

                int a = 6, b = 4;
                int z = a + b; // '+' is the operator in this example.

The table below represents the arithmetic operator available in C#:

OperatorDescriptionExample
+To adds two operands.a + b
-To Subtracts second operand from the first.a - b
*To multiplies the operands.a * b
/To divides numerator by de-numerator.a / b
%To modulus Operator and remainder of after an integer division.a % b
++To Increment operator increases integer value by one.a++
--To Decrement operator decreases integer value by one.a--


More example of arithmetic operators

  class Program
    {
        static void Main(string[] args)
        {
            int a;
            int b;
            int c = 0;
            a = 10;
            b = 5;
            c = a + b;
            // +
            Console.WriteLine("Example of Addition operator:");
            Console.WriteLine("a + b = " + c);

            // -
            c = a - b;
            Console.WriteLine("Example of Subtraction operator:");
            Console.WriteLine("a - b = " + c);

            // *
            c = a * b;
            Console.WriteLine("Example of Multiplication operator:");
            Console.WriteLine("a * b = " + c);

            // /
            c = a / b;
            Console.WriteLine("Example of Division operator:");
            Console.WriteLine("a / b = " + c);

            // %
            c = a % b;
            Console.WriteLine("Example of Modulus operator:");
            Console.WriteLine("a % b = " + c);

            // ++
            c = ++a;
            Console.WriteLine("Example of Increment operator:");
            Console.WriteLine("++a = " + c);

            // --
            c = --b;
            Console.WriteLine("Example of Decrement operator:");
            Console.WriteLine("--a = " + c);

            Console.ReadKey();

        }
    }

Output

Relational Operators

Relational operators are used to check a relation between two oprends. The output is always true or false.

Example of rational operatos

            int x = 10;
            int y = 20;
            Boolean isEqual = (x == 20); // the value of isEqual is false

The table below represents the Rational operator available in C#:

OperatorDescriptionExample
=='To checks if the values of two operands are equal.a == b
!=To Checks if the values of two operands are not equal.a != b
>To Checks if the value of left operand is greater than the value of right operand.a > b
<To Checks if the value of left operand is less than the value of right operand.a < b
>=To Checks if the value of left operand is greater than or equal to the value of right operand.a >= b
<=To Checks if the value of left operand is less than or equal to the value of right operand.a <= b

Comparison operator

The comparison operator is to compare two operands. It returns a true or false, based on the value of the operands being compared.

The table below lists the complete Comparison operators in C#:

Let use the ‘x’ variable in the table below, assuming it’s equal to 5.

OperatorDescriptionExample
<Less thanx < 5 (returns true)
>Greater thanx > 6 (returns false)
<=Less than equal tox <= 5 (returns true)
>=Greater than equal tox >= 5 (returns true)
==Equal equal tox == 5 (returns true)
!=Not equal tox != 5 (returns false)

Assignment Operators

Assignment operators are used to assign values to variables. The following table contains the most used assignment operators in C#:

OperatorDescriptionExample
=Simplest and most used assignment operator. Assigns values from right side operands to left side operand.x = 1 + 1. result: x = 2
+=Add and assignment operator. Adds right operand to the left operand then assign the result to left operandx += 1, same as x = x + 1
-=Subtract and assignment operator. Subtracts right operand from the left operand then assign the result to left operandx -= 1, same as x = x - 1
*=Multiply and assignment operator. Multiplies right operand with the left operand and assign the result to left operandx *= 2, same as x = x * 2
/=Divide and assignment operator. Divides left operand with the right operand then assign the result to left operandx /= 2, same as x = x / 2
%=Modulus and assignment operator. Takes modulus using two operands then assign the result to left operandx %= 2, same as x = x % 2

Example of assignment operators

 static void Main(string[] args)
        {
            int x;
            x = 5;
            // =
            Console.WriteLine("Example of simple assignment operator:");
            x = 5 + 5;
            Console.Write("x = 5 + 5, ");
            Console.WriteLine ("x = " + x);

            // +=
            x = 5;
            x += 1;
            Console.WriteLine("Example of += operator:");
            Console.Write("x += 1, ");
            Console.WriteLine("x =" + x);

            // -=
            x = 5;
            x -= 1;
            Console.WriteLine("Example of -= operator:");
            Console.Write("x -= 1, ");
            Console.WriteLine("x = " + x);
        
            // *=
            x = 5;
            x *= 2;
            Console.WriteLine("Example of *= operator:");
            Console.Write("x *= 2, ");
            Console.WriteLine("x = " + x);

            // /=
            x = 5;
            x /= 2;
            Console.WriteLine("Example of /= operator:");
            Console.Write("x /= 2, ");
            Console.WriteLine("x = " + x);

            // %=
            x = 5;
            x %= 2;
            Console.WriteLine("Example of %= operator:");
            Console.Write("x %= 2, ");
            Console.WriteLine("x = " + x);

            Console.ReadKey();

        }

Output:

Logical Operators 

Logical operators are used to perform logical operations between two operands. The operation always returns a Boolean value. The operators are used mainly in if statements and loops.

In C#, the representation of ‘and’ and ‘or’ are && and ||, respectively.

Below are the results returned from evaluation two operands using the operators “Or” and “And”:

OperatorDescriptionExample
&&Logical “AND” operator. If one of the operands is false, then the result is false.(a && b)
||Logical “OR” Operator. If any of the two operands if true, then the result is true.(a || b)
!Logical “NOT” Operator. This operator is uses to reverse the logic. If result is true, this operator will reserve it to false, and vise versa.!(a && b)

Bitwise Operators

Bitwise operators deal with bits on a bit to bit basis. The three Bitwise operators are:

  • Bitwise OR |

Binary’ OR’ Operator. It copies a bit if it exists in either operand.

  • Bitwise AND &

Binary ‘AND’ Operator. It copies a bit to the result if it exists in both operands.

  • Bitwise XOR ^

Binary ’ XOR’ Operator. It copies the bit if it exists in one operand but not both.

  • Bitwise Left Shift <<

Binary ‘Left Shift’ Operator. The left operands value is shifted left by the number of bits specified in the right operand.

  • Bitwise Right Shift >>

Binary ‘Right Shift’ Operator. The left operands value is shifted Right by the number of bits specified in the right operand.

Example of bitwise operator

        static void Main(string[] args)
        {
            int x = 30;            
            int y = 9;          
            int z = 0;

            z = x & y;          
            Console.WriteLine("z = x & y  value is {0}", z);

            z = x | y;             
            Console.WriteLine("z = x | y value is {0}", z);

            z = x ^ y;           
            Console.WriteLine("z = x ^ y  value is {0}", z);

            z = ~x;              
            Console.WriteLine("z = ~x value is {0}", z);

            z = x << 2;   
            Console.WriteLine("z = x << 2 value is {0}", z);

            z = x >> 2;     
            Console.WriteLine("z = x >> 2 value is {0}", z);
            Console.ReadLine();

            Console.ReadKey();

        }

Miscellaneous Operators

Other important operators in C# including sizeof, typeof and &:

OperatorDescriptionExample
sizeof()Returns the size of a data typesizeof(decimal), returns16.
typeof()Returns the type of a classtypeof(String);
&Returns the address of a variable.&x; returns address of the variable.

Leave a Reply

Your email address will not be published. Required fields are marked *