As you know, operators play an important role in arithmetic; In the same way, operators have an extraordinary role in programming languages. The concept remains the same, but some new phenomena can be used for building logic and codes with a C# program using these operators. In this chapter, you will learn about all the operators of C#.



What Are the Operators in C#?

Operators are symbols in a programming language that tell the compiler or interpreter to perform specific operations on operands to produce the final output or result.

There are six different types of operators provided by C#. These are:
  • Arithmetic Operators
  • Logical Operators
  • Relational Operators
  • Assignment Operators
  • Bitwise Operators
  • Misc Operators

Let's know about each operator in detail:

Arithmetic Operators

These sets of operators are used for performing arithmetic as well as increment/decrement operations. These are:

Operator Description
Addition The '+' operator is used to add two operands. Like: x + y.
Subtraction The '-' operator is used to subtract two operands. Like x - y.
Multiplication The '*' operator is used to multiply two operands. Like: x * y.
Division The '/' operator is used to divide the first operand by the second one. Like: x / y.
Modulus The '%' operator is used to return a remainder when the first operand is divided by the second one. Like: x % y.
Increment The '++' operator is used to increase the integer value by 1. Like: x++ or ++x.
Decrement The '--' operator is used to decreasing the integer value by 1. Like: x-- or --x.

Logical Operators

Logical operators combine two or more conditions and evaluate a logical decision. These operators are:

Operator Description
Logical AND (&&) Logical AND (&&) operator returns true if both the conditions/operands satisfy; otherwise, returns false.
Logical OR (||) Logical OR (||) operator returns true if anyone or even both of the conditions/operands satisfy; otherwise, returns false.
Logical NOT (!) Logical NOT (!) operator returns true if the condition is not satisfied; otherwise, returns false.

Relational Operators

These sets of operators are used to relate two operands or calculate a relation between two operands. These operators are:

Operator Description
Equal To operator (==) Equal To operator (==) operator is used to check if two operands are equal or not. If so, it returns true, otherwise false. Like: 6==6 will return true.
Not Equal To (!=) Not Equal To (!=) operator is used to checking if two operands are equal or not. If not, it returns true, otherwise false. Like: 6!=2 will return true.
Greater Than (>) Greater than (>) operator is used to check whether the first operand is larger than the second. If so, returns true, otherwise false. Like: 8> four will return true.
Less Than (<) Less Than (<) operator is used to check if the first operand is smaller than the second. If so, returns true, otherwise false. Like: 8<1 will return false.
Greater Than Equal To (>=) Greater Than Equal To (>=) operator is used to check if the first operand is larger than or equal to the second. If so, returns true, otherwise false. Like: 2>=7 will return false.
Less Than Equal To Less Than Equal To (<=) operator is used to checking if the first operand is smaller than or equal to the second. If so, returns true, otherwise false. Like: 6<=6 will return true.

Bitwise Operators

These operators work at a bit level to execute bit by bit operations. These operators are:

Operator Description
Bitwise AND (&) Bitwise AND (&) operator takes two operands and does AND operation on every bit of those numbers.
Bitwise OR (|) Bitwise OR (|) operator takes two operands and does OR operation on every bit of those numbers.
Bitwise XOR (^) Bitwise XOR (^) operator takes two operands and does XOR operation on every bit of those numbers.
Bitwise Left Shift (<<) Bitwise Left Shift (<<) operator takes two operands and does left shifting of bits on the two operands and determines the number of places to shift.
Bitwise Right Shift (>>) Bitwise Right Shift (>>) operator takes two operands and does the right shifting of bits on the two operands and determines the number of places to shift.

Assignment Operators

These operators are used to assign values to variables with different operations. These are:

Operator Description
= operator = operator assigns the value of the right side operand to its left side operand. Like: g = s.
+= Operator += operator adds the value of the variable on the left with the value on the right, which is (result) then assigned to the variable that is on the left.
-= Operator -= operator subtracts the value of the variable on the left with the value on the right, which is (result) then assigned to the variable that is on the left.
*= Operator *= operator multiplies the value of the variable on the left with the value on the right, which is (result) then assigned to the variable that is on the left.
/= Operator /= operator divides the value of the variable on the left with the value on the right, which is (result) then assigned to the variable that is on the left.
%= Operator %= operator first modulo the current value of the variable on the left with the value on the right and (the result) then assigned to the variable that is on the left.
<<= Operator <<= is the left shift assignment operator that will first left shift the current value of the variable on the left with the value on the right, and the result is then assigned to the variable that is on the left.
>>= Operator >>= is the right shift assignment operator that will first right shift the current value of the variable on the left with the value on the right, and the result is then assigned to the variable that is on the left.
&= Operator &= is the Bitwise AND operator that will first perform a "Bitwise AND" with the current value of the variable on the left with the value on the right, and the result is then assigned to the variable that is on the left.
^= Operator ^= is a Bitwise Exclusive OR operator that will first perform a "Bitwise Exclusive OR" with the variable's current value on the left with the value on the right, and the result is then assigned to the variable that is on the left.
|= Operator |= is a Bitwise Inclusive OR operator that will first perform a "Bitwise Inclusive OR" with the variable's current value on the left with the value on the right, and the result is then assigned to the variable that is on the left.

Other Miscellaneous Operators

Operator Description
sizeof() sizeof() is used to return the data type size.
typeof() typeof() is used to return the class type.
Pointer to a variable (*) operator Pointer to a variable (*) operator is used to point to a variable.
Address of (&) operator: Address of (&) operator is used to return the variable's address.
Conditional operator (?:) Conditional operator (?:) determines if any condition is true? If yes, then first value: otherwise second value.
is: "is:" finds out if an object is of a specific type or not.


Found This Page Useful? Share It!
Get the Latest Tutorials and Updates
Join us on Telegram