C# Operators

An operator is a symbol that tells the computer to perform certain mathematical or logical manipulations. It is used to manipulate data and variables in programs. In C#, it supports a rich set of operators, there are 8 types of operators are given below:

  • Arithmetic Operators
  • Relational Operators
  • Logical Operators
  • Increment and decrement Operators
  • Assignment Operators
  • Conditional Operators
  • Bitwise Operators
  • Special Operators
  • Arithmetic Operators:

    C# provides all basic arithmetic operators. These operator can operate on any built in numeric data type. We can’t use these operators on Boolean type. They are listed in table:

    OperatorMeaning
    +Addition or unary plus
    -Subtraction or unary minus
    *Multiplication
    /Division
    %Modulo division

    Relational Operators:

    When we often compare two quantities and depending on their relation to take certain decisions that is called Relational Operators. C# supports six Relational Operators and which are listed in table:

    OperatorDescription
    <is less than
    <=is less than or equal to
    >is greater than
    >=is greater than or equal to
    (==)is equal to
    !=is not equal to

    Logical Operators:

    The Logical Operators && and || are used when we want to form compound conditions by combining two or more relations. C# supports six Logical Operators which are listed in table:

    OperatorDescription
    &&logical AND
    ||logical OR
    !logical NOT
    &bitwise logical AND
    |bitwise logical OR
    ^bitwise logical exclusive OR

    Increment and decrement Operators:

    In C#, there are two useful operators are used and that is Increment and decrement Operators. Increment operator (++) is used to increase the value of an integer by 1. Decrement operator (- -) is used to reduce the value of an integer by 1. It has the following form:

    x = 5;
    x++ or ++x will produce the result x = 6
    y = 5;
    y- – or – -y will produce the result y = 4
    

    Assignment Operators:

    Assignment Operators are used to assign the value of an expression to a variable. It has the following syntax:

    v op=exp
    

    where v is a variable, exp is an expression, op is a C# binary operator. The operator op is also known as Shorthand Assignment Operator. It has the following syntax:
        v op=exp
    It is equivalent to 
        v=v op(exp);
    

    Example:
        x+=y+1;
    It is equivalent to 
        x=x+(y+1);
    

    Conditional Operators:

    The character pair ?: is a ternary operator. This operator is used to construct conditional expressions, So it is called Conditional Operator. It has the following Syntax:

    exp1 ? exp2 : exp3
    

    where exp1,exp2,exp3 are expressions. The operator ?: works as follows:
    exp1 is evaluated first, it is true. then exp2 is evaluated and becomes the value of the conditional expression. If exp1 is false, exp3 is evaluated and its value becomes the value of the conditional expression.
    Example:
    x=5;
    y=6;
    p=(x>y)?x:y;
    

    Bitwise Operators:

    In C#, Bitwise Operators are used to manipulate of data at bit level. These operators may be used for testing the bits or shifting them to the right or left. Bitwise operator may not be applied to floating-point data types.

    Special Operators:

    In C#, there are 8 types of Special Operators are exists given as below:

    1. is -
    2. as
    3. typeof  (type operator)
    4. sizeof  (size operator)
    5. new
    6. .(dot)
    7. checked
    8. unchecked