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
1 2 3 |
int a = 3; int b = 5; int result = a + b; |
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:-
- Arithmetic Operators (+ – * / %)
- Assignment Operators
- Bitwise Operators
- Logical Operators
- Relational or Comparison Operators
- Ternary Operators
- Unary Operators
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#).
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
using System; namespace Demo { class Program { static void Main(string[] args) { int a = 8; int b = 2; int result; //Addition result = a + b; Console.WriteLine("Addition of a and b is: "+result); //Subtraction result = a - b; Console.WriteLine("Subtraction of a and b is: "+result); //Multiplication result = a * b; Console.WriteLine("Multiplication of a and b is: "+result); //Division result = a / b; Console.WriteLine("Division of a and b is: "+result); //Modulus result = a % b; Console.WriteLine("Modulus of a and b is: "+result); } } } |
Output
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.
1 |
int a = 9; |
However, the other operators like addition assignment (+=
) can be used to add the number with the assignment.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
using System; namespace Demo { class Program { static void Main(string[] args) { int a = 9; a += 9; Console.WriteLine("The addition assignment result is: "+a); } } } |
Output
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
using System; namespace Demo { class Program { static void Main(string[] args) { int a; //Assignment a = 9; Console.WriteLine("Assignment is: "+a); //Addition assignment a += 9; Console.WriteLine("Addition assignment of a is: "+a); //Subtraction assignment a -= 9; Console.WriteLine("Subtraction assignment of a is: "+a); //Multiplication assignment a *= 9; Console.WriteLine("Multiplication assignment of a is: "+a); //Division assignment a /= 9; Console.WriteLine("Division assignment of a is: "+a); //Modulo assignment a %= 9; Console.WriteLine("Modulo assignment of a is: "+a); //Bitwise AND assignment a &= 9; Console.WriteLine("Bitwise AND assignment of a is: "+a); //Bitwise OR assignment a |= 9; Console.WriteLine("The bitwise OR assignment of a is: "+a); //Bitwise XOR assignment a ^= 9; Console.WriteLine("Bitwise XOR assignment of a is: "+a); //Bitwise Left Shift assignment a <<= 9; Console.WriteLine("The bitwise left shift assignment of a is: "+a); //Bitwise Right Shift assignment a >>= 9; Console.WriteLine("Bitwise right shift assignment of a is: "+a); } } } |
Output
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
using System; namespace Demo { class Program { static void Main(string[] args) { int a = 6; int b = 12; //declare result variable to store output int result; //Bitwise Complement result = ~a; Console.WriteLine("Bitwise complement of a is: "+result); //Bitwise AND result = a & b; Console.WriteLine("The Bitwise AND of a and b is: "+result); //Bitwise OR result = a | b; Console.WriteLine("Bitwise OR of a and b is: "+result); //Bitwise XOR result = a ^ b; Console.WriteLine("The Bitwise XOR of a and b is: "+result); //Bitwise Left shift result = a << b; Console.WriteLine("Bitwise Left shift of a and b is: "+result); //Bitwise Right shift result = a >> b; Console.WriteLine("The Bitwise Right shift of a and b is: "+result); } } } |
Output
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) |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
using System; namespace Demo { class Program { static void Main(string[] args) { int a = 8; int b = 12; bool result; //Logical AND result = a < 3 && b > 9; Console.WriteLine("Logical AND is: "+result); //Logical OR result = a < 3 || b > 9; Console.WriteLine("The logical OR is: "+result); //Logical NOT result = !(a < 3); Console.WriteLine("Logical NOT is: "+result); } } } |
Output
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
using System; namespace Demo { class Program { static void Main(string[] args) { int a = 9; int b = 11; bool result; //Equal to result = (a == b); Console.WriteLine("Equal to of a and b is: "+result); //Not equal to result = (a != b); Console.WriteLine("Not equal to of a and b is: "+result); //Greater than result = (a > b); Console.WriteLine("Greater than of a and b is: "+result); //Less than result = (a < b); Console.WriteLine("Less than of a and b is: "+result); //Greater than or equal to result = (a >= b); Console.WriteLine("Greater than or equal to of a and b is: "+result); //Less than or equal to result = (a <= b); Console.WriteLine("Less than or equal to of a and b is: "+result); } } } |
Output
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.
1 |
condition? statement1 : statement2 |
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
using System; namespace Demo { class Program { static void Main(string[] args) { int a = 6; int b = 10; string result; //Ternary condition result = (a > b)? "a is greater than b" : "a is less than b"; Console.WriteLine("Ternary condition is: "+result); } } } |
Output
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
using System; namespace Demo { class Program { static void Main(string[] args) { int a = 3; int result; //Increment before result = ++a; Console.WriteLine("Increment before of a is: "+result); //Increment after result = a++; Console.WriteLine("Increment after of a is: "+result); //Decrement before result = --a; Console.WriteLine("Decrement before of a is: "+result); //Decrement after result = a--; Console.WriteLine("Decrement after of a is: "+result); } } } |
Output
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