Operators in C#

As you know operators play a significant role in arithmetic, the same way operators have an extraordinary role in programming languages. The concept remains the same, but there are some new phenomenon's that 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 tells the compiler or interpreter to perform specific operations on operands for producing 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 set of operators are used for performing arithmetic as well as increment/decrement operations. These are:

OperatorDescription
AdditionThe '+' operator is used to add two operands. Like: x + y.
SubtractionThe '-' operator is used to subtract two operands. Like x - y.
MultiplicationThe '*' operator is used to multiply two operands. Like: x * y.
DivisionThe '/' operator is used to divide the first operand by the second one. Like: x / y.
ModulusThe '%' operator is used to return a remainder when the first operand is divided by the second one. Like: x % y.
IncrementThe '++' operator is used to increase the integer value by 1. Like: x++ or ++x.
DecrementThe '--' operator is used to decreasing the integer value by 1. Like: x-- or --x.

Logical Operators

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

OperatorDescription
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 set of operators are used to relate two operands or calculate a relation between two operands. These operators are:

OperatorDescription
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 ToLess 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' works at a bit level to execute bit by bit operations. These operators are:

OperatorDescription
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 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:

OperatorDescription
= operator= operator assigns the value of 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 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 Inclusive OR operator that will first perform a "Bitwise Inclusive OR" 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.

Other Miscellaneous Operators

OperatorDescription
sizeof()sizeof() is used to return the data type size.
typeof()typeof() is used to return the class type.
Pointer to a variable (*) operatorPointer 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 certain type or not.

Scroll Back to Top