Operators in C sharp



Operators are used to perform Mathematical Operations

  • Arithmetic Operators-
  • Operator Usage Example
    + To add two Variables A+B
    - To Substract Two Variables A-B
    * To Multiply Two Variables A*B
    / To Divide Two Variables A/B
    % Called As Modules. Gives Reminder of Division A%B
    ++ Increment Operator-Increments integer variable by 1 A++
    -- Decrement Operator-Decrements integer variable by 1 A--


  • Relational Operators-
  • Operator Usage Example
    == To check if two operands are equal A==B
    != To check if two operands are equal or not A!=B
    > To Check if the left hand operand is grater then right A > B
    < To check whether left hand operand is less than right A < B
    >= Check whether left hand operand is greater than or equal to right hand operand A>=B
    <= Check whether left hand operand is less than or equal to right hand operand A<=B


  • Logical Operators-
  • Operator Usage Example
    && Logical AND Operator. If both values are non-zero then condition is true A && B
    || Logical AND Operator. If any one value is non-zero then condition is true A || B
    ! Logical NOT Operator. Used to reverse the logical state of operand !A


  • Bitwise Operators-

  • Operator Usage Example
    & Compares bits of two integrals respectively and set corresponding bit
    to 1 when both sides have 1 bit
    A & B
    | Compares bits of two integrals respectively and set corresponding bit
    to 1 when either sides have 1 bit
    A | B
    ^ XOR operator.Compares bits of two integrals respectively and set corresponding
    bit to 1 when only one sides have 1 bit
    A ^ B
    ~ Unary operator resulting in one's complement of number ~A
    << Left Shift- left operand value is moved left specified by
    bits specified at right of operator
    A << 5
    >> Right Shift- left operand value is moved right specified by
    bits specified at right of operator
    A >>4



  • Assignment Operators-

  • Operator Usage Example
    = Assignment operator used in assigning value of right side of operator to left side operand A = 2 Or R=P+Q
    += Shorthand Assignment operator
    First Adds the numbers and then assign the result to left hand operand
    A+=B
    -= Shorthand Assignment operator
    First subtracts the numbers and then assign the result to left hand operand
    A -= B
    *= Shorthand Assignment operator
    First multiply the numbers and then assign the result to left hand operand
    A*=B
    /= Shorthand Assignment operator
    First divide the numbers and then assign the result to left hand operand
    A /= B
    %= Shorthand Assignment operator
    First takes modules the numbers and then assign the result to left hand operand
    A %=B

    Operators in C sharp Operators in C sharp Reviewed by vishal on July 30, 2017 Rating: 5

    No comments