C# Operators

In this tutorial, you will learn all about C-sharp operators and their different types with examples. An operator is a symbol that is used to perform operations over operands.

Operands are the variables of some data type that may contain some value to operate using operators. There are numerous operators in C# that you will learn here.

Let’s see a simple example of plus (+) operator as given below

Here, in the above example, a and b are the operands and + is the addition operator to manipulate the values of these variables. The manipulation does the sum of values 3 and 5 that brings out the result 8.

The plus (+) operator can be used to add two variables or two numbers to store in a variable. Similarly, there are many other operators with different uses that you will learn here below.

Types of Operators in C-sharp (C#)

Below is the list of operator types in C# that you will learn here with examples:-

Let’s find out these types of operators in more detail below.

Arithmetic Operators in C-sharp (C#)

Arithmetic operators can be used to perform mathematical operations like addition, subtraction, division, multiplication, etc. Below is the list of arithmetic operators in C#. After that, you will also get examples of each arithmetic operator.

Operator Operator Name Example
+ Addition a + b
Subtraction a – b
* Multiplication a * b
/ Division a / b
% Modulus (Remainder) a % b

Let’s see these operators with examples to understand the use in C-sharp (C#).

Output

Addition of a and b is: 10
Subtraction of a and b is: 6
Multiplication of a and b is: 16
Division of a and b is: 4
Modulus of a and b is: 0

The above output shows the manipulation of the values of two variables and stores the result in the other variable. All the variables in the above example are of int data type that stores integer type values.

Assignment Operators C-sharp (C#)

Assignment operators can be used to assign a value to a variable in C#. The simple equals to (=) operator is useful to assign a value to the variable as given below.

However, the other operators like addition assignment (+=) can be used to add the number with the assignment.

Output

The addition assignment result is: 18

The above example first declares the variable of int type with a value assigned using the assignment operator =. After that, it uses the addition assignment operator += to add the number with an assignment.

Let’s find out some other assignment operators in C-sharp (C#) with examples.

Operator Operator Name Example
= Assignment a = 9
+= Addition Assignment a += 9
-= Subtraction Assignment a -= 9
*= Multiplication Assignment a *= 9
/= Division Assignment a /= 9
%= Modulo Assignment a %= 9
&= Bitwise AND Assignment a &= 9
|= Bitwise OR Assignment a |= 9
^= Bitwise XOR Assignment a ^= 9
<<= Bitwise Left Shift Assignment a <<= 9
>>= Bitwise Right Shift Assignment a >>= 9

The examples of each assignment operator are given below.

Output

Assignment is: 9
Addition assignment of a is: 18
Subtraction assignment of a is: 9
Multiplication assignment of a is: 81
Division assignment of a is: 9
Modulo assignment of a is: 0
Bitwise AND assignment of a is: 0
The bitwise OR assignment of a is: 9
Bitwise XOR assignment of a is: 0
The bitwise left shift assignment of a is: 0
Bitwise right shift assignment of a is: 0

You can check each assignment operator with the above examples to learn the uses of these operators in C#.

Bitwise Operators in C-sharp (C#)

Bitwise operators can be used to perform bit operations in C#. There are 4 bitwise and 2-bit shift operators to perform an operation over operands.

The bitwise operators are given below with examples.

Operator Operator Name Example
~ Bitwise Complement ~a
& Bitwise AND a & b
| Bitwise OR a | b
^ Bitwise XOR (Exclusive OR) a ^ b
<< Bitwise Left shift a << b
>> Bitwise Right shift a >> b

Let’s find out the use of these bitwise operators with examples given below. The example contains the manipulation over all the bitwise operators to help you easily understand the use.

Output

Bitwise complement of a is: -7
The Bitwise AND of a and b is: 4
Bitwise OR of a and b is: 14
The Bitwise XOR of a and b is: 10
Bitwise Left shift of a and b is: 24576
The Bitwise Right shift of a and b is: 0

The output above is the bit operation result of the manipulation of two values stored in the variable a and b.

Logical Operators in C-sharp (C#)

Logical operators can be used to perform logical operations for decision-making. This operation gives results in the boolean format value that are True and False. This is useful to check the conditions if true or false to execute statements in a loop until the result is true or false.

Below are the list of logical operators in C#.

Operator Operator Name Example
&& Logical AND a < 3 && b > 9
|| Logical OR a < 3 || b > 9
! Logical NOT !(a < 3)

Output

Logical AND is: False
The logical OR is: True
Logical NOT is: True

When you check the above results, the output of the logical operators over operands gives True or False. Logical operators are useful when you want to run a loop until the given condition is true.

Relational or Comparison Operators

Relational or comparison operators can be used to compare the two values or check the relation between two values. The comparison of two values gives a result in the boolean format that is either True or False.

We can use comparison operators when making decisions in loops. If the comparison is true, you will get results as True. Otherwise, you will get results as False.

Below is the list of relational operators with examples and descriptions.

Operator Operator Name Example
== Equal to a == b
!= Not equal to a != b
> Greater than a > b
< Less than a < b
>= Greater than or equal to a >= b
<= Less than or equal to a <= b

Let’s find out the use of these relational operators with examples of each one given below.

Output

Equal to of a and b is: False
Not equal to of a and b is: True
Greater than of a and b is: False
Less than of a and b is: True
Greater than or equal to of a and b is: False
Less than or equal to of a and b is: True

The above examples show that the result of the comparison operator is boolean. The boolean value can be either True or False. It is useful in decision making to run the loop until the comparison given boolean value True.

Ternary Operators

Ternary operators can be used to perform operations over three operands. It first checks whether the condition is true using the operator ?. After that, The two expressions are given with a colon (:) between them. If the condition is true, it prints the first statement. If the condition is false, it prints the second statement.

The description of the Ternary operator is given below with an example.

Operator Operator Name Example
? : Ternary condition (a < b)? “a is greater than b” : “a is less than b”

Let’s see an example to easily understand the use of the Ternary operator.

Output

Ternary condition is: a is less than b

In the above example, there are two variables that contain the values. It first checks whether the value 6 is greater than 10 or not. If the condition is true, it prints the first statement in the output.

Unary Operators

Unary operators are the operator that can be used to perform operations over a single operator in C#. There are two unary operators that are double plus++ double minus-- as given below.

Operator Operator Name Example
++ Increment ++a or a++
Decrement –a or a–

Let’s see these two operators and learn the use with the example given below.

Output

Increment before of a is: 4
Increment after of a is: 4
Decrement before of a is: 4
Decrement after of a is: 4

In the above example, The before increment (++a)/decrement --a operator increment/decrement the variable value first. After that, it prints the incremented/decremented value in the output

While the after increment (a++)/decrement a-- operator increment/decrement the variable value after printing the result.

Related Tutorials

Reference

Leave a comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.