Operators in C#

Introduction to C# operators

C# operators are the symbols that are used for different operations like mathematical expressions, function calls, etc.

There are different kinds of operators.

  1. Assignment operators
  2. Arithmetic operators
  3. Conditional operators
  4. Unary operators
  5. Increment & Decrement operators
  6. Relational operators
  7. Logical operators

Assignment operator

  1. “=”: This is called the assignment operator. “=” assigns the variable on the left of it with the value on the right of it. In other words, it provides the data on its R.H.S. to the variable in its L.H.S.
    Example. X=9;
    Here, the operator”=” assigns value 9 to x.

Arithmetic operator

The arithmetic operator is used for mathematical operations. There are 5 types of arithmetic operators.

  1. +: This operator is called an additional operator. It is used for the addition of two pieces of data.
    Example. z=x+y;
    Here z will result in the addition of two variables x & y.
  2. -: This operator is called the subtraction operator. It is used for the difference of two pieces of data.
    Example. z=x-y;
    Here z will result in a difference of two variables x & y.
  3. *: This operator is called the multiplication operator. It is used for product of two data.
    Example. z=x*y;
    Here z will result in the product of two variables x & y.
  4. /: This operator is called division operator. It is used for the division of two data.
    Example. z=x/y;
    Here z will result in a quotient for the division of two variables x & y.
  5. %: This operator is called the modulus operator. It is used for finding the remainder after dividing one data from another.
    Example. z=x%y;
    Here z will result in the remainder after the division of two variables x & y.

Conditional operator

Conditional operators are the operators used to check a condition. The types of conditional operators are

  1. “ ?": This is called a ternary operator. This is used instead of the if else statement. It avoids long writing of code for if-else statements if the if-else statement is of one statement only like
    if (something)
    {
        Dosomething;
    }
    else
    {
        Dosomething_else;
    }
    
    Then instead of writing this much longer code, we can write
    Result = x?y:z;
    This means that if the input is x then results in y else results in z.
  2. “ &&”: This is also called logical AND. If the first operand is false, then C# does not evaluate the second operand. It will operate only when both the operands are true.
    Example. X&&y
    Here, it is checked whether both the operands are true or not.
  3. “||”: This is also called logical OR. If the first operand is true, then C# does not evaluate the second operand. It will operate if any of the operands is true.
    Example X||y
    Here, it is checked whether any of the operands are true or not.
  4. “??“: This is also called the Null coalescing operator. This returns the value on its left if it is non-null; otherwise, returns the value on the right of it. Example: X??y Here, this returns x if it is non-null; otherwise, returns y.

Unary operator

Unary operators work on a single operand. The types of unary operators are

  1. +x: It returns the positive value of x.
  2. -x: It does the numeric negation of x.
  3. !x: It does the logical negation of x.
  4. ~x: It is used for the bitwise complement of x.

Increment & decrement operator

Increment & decrement operators are used for incrementing & decrementing the count of data by

  1. ++x: It is also called prefix increment. It returns the value of x after updating the storage location with the value of x that is one greater (typically adds the integer 1).
  2. --x: It is also called prefix decrement. It returns the value of x after updating the storage location with the value of x that is one less (typically adds the integer 1).

Relational operator

Relational operators are used for establishing relationships between two different data. The types of relational operators are

  1. >: This symbol is used when the data on the left of it is greater than the data on its right.
    Example. x>y;
    Here, the relation says that the value of x is greater than the value of y.
  2. <: This symbol is used when the data on the left of it is smaller than the data on its right.
    Example. X<y;
    Here, the relation says that the value of x is smaller than the value of y.
  3. >=: This symbol is used when the data on the left of it is greater or equal to than the data on its right.
    Example. x>=y;
    Here, the relation says that the value of x is greater than or equal to the value of y.
  4. <=: This symbol is used when the data on the left of it is smaller than or equal to the data on its right.
    Example. x<=y;
    Here, the relation says that the value of x is smaller than or equal to the value of y.
  5. == : This symbol is used when the data on the left of it is equal to the data on its right.
    Example. X==y;
    Here, the relation says that the value of x is equal to the value of y.
  6. !=: This symbol is used when the data on the left of it is not equal to the data on its right.
    Example. X!=y;
    Here, the relation says that the value of x is not equal to the value of y.

Logical operator

Logical operators are used to do logical operations. The types of logical operators are

  1. x & y: It is also called logical or bitwise AND. It is used with integer types and generally allowed with enum types for doing the logical & operations.
  2. x ^ y : It is also called logical or bitwise XOR. We can generally use this with integer types and enum types for doing the logical XOR operations.
  3. x | y: I*t is also called logical or bitwise OR. It is used with integer types and enum types for doing logical OR operations.

I hope this article has helped you.