In C# programming language, an operator is an element that takes one or more operands to perform some computation. Basically, there are three kinds of operators are there in C#.
On the basis of "number of operands", the operators are classified into the following categories.
- Unary operators : Operators that take one operand are called Unary operators (++, --, etc.)
- Binary operators : The operators that take two operands are called Binary operators (+, -, =, /, ==, etc.).
- Ternary operators : The operators that take three operands are called Ternary operators. There is only one such operator in C# which is the conditional operator (?:).
To understand them in an easy way, we categorize the the operators on the basis of "type of operations" that these operators perform. Let's explore each in detail.
Arithmetic operators
These operators perform the arithmetic computation over the operand(s).
Operator | Name | Description | Example |
---|---|---|---|
+ | Addition | Add two operands | x + y |
- | Subtraction | Subtract second operand from first | x - y |
* | Multiplication | Computes the multiplicative product of two operands | x * y |
/ | Division | Divides first operand with second operand | x / y |
% | Modulus | Returns the remainder of the division operation between two operands | x % y |
++ | Increment | This unary operator increases the value of its operand by 1. | x++ (post-increment) or ++x (pre-increment) |
-- | Decrement | This unary operator decreases the value of its operand by 1. | x-- (post-decrement) or --x (pre-decrement) |
using System;
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Arithmetic Operators");
Console.WriteLine("");
int x = 25;
int y = 10;
Console.WriteLine("");
Console.Write("x = " + x);
Console.WriteLine(" and y = " + y);
Console.WriteLine("");
Console.WriteLine("Add");
Console.Write("x + y = ");
Console.WriteLine(x + y);
Console.WriteLine("");
Console.WriteLine("Subtract");
Console.Write("x - y = ");
Console.WriteLine(x - y);
Console.WriteLine("");
Console.WriteLine("Multiply");
Console.Write("x * y = ");
Console.WriteLine(x * y);
Console.WriteLine("");
Console.WriteLine("Divide");
Console.Write("x / y = ");
Console.WriteLine(x / y);
Console.WriteLine("");
Console.WriteLine("Modulus");
Console.Write("x % y = ");
Console.WriteLine(x % y);
Console.WriteLine("");
Console.WriteLine("Increment");
Console.WriteLine("");
Console.WriteLine("Pre increment");
Console.WriteLine("Before pre-increment x = " + x);
Console.Write("During pre-increment : ++x = ");
Console.WriteLine(++x);
Console.WriteLine("After pre-increment x = " + x);
Console.WriteLine("");
Console.WriteLine("Post increment");
Console.WriteLine("Before post-increment x = " + x);
Console.Write("During post increment : x++ = ");
Console.WriteLine(x++);
Console.WriteLine("After post-increment x = " + x);
Console.WriteLine("");
Console.WriteLine("Decrement");
Console.WriteLine("Before pre-decrement x = " + x);
Console.Write("During pre-decrement : --x = ");
Console.WriteLine(--x);
Console.WriteLine("After pre-decrement x = " + x);
Console.WriteLine("");
Console.WriteLine("Post decrement");
Console.WriteLine("Before post-decrement x = " + x);
Console.Write("During post-decrement : x-- = ");
Console.WriteLine(x--);
Console.WriteLine("After post-decrement x = " + x);
Console.WriteLine("");
Console.Write("Press any key to continue...");
Console.ReadKey(true);
}
}
Relational operators
Relational Operators evaluate the comparison between the operands. C# supports the following relational operators.
Operator | Name | Description | Example |
---|---|---|---|
== | Equals to | It returns true only when operands are equal; false otherwise. | x == y |
< | Less than | It returns true only when first operand is less than second operand; false otherwise. | x < y |
<= | Less than or equals to | It returns true only when first operand is less than or equals to second operand; false otherwise. | x <= y |
> | Greater than | It returns true only when first operand is greater than second operand; false otherwise. | x < y |
>= | Greater than or equals to | It returns true only when first operand is greater than or equals to second operand; false otherwise. | x >= y |
!= | Not equals to | It returns true only when first operand is not equals to second operand; false otherwise. | x != y |
using System;
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Relational Operators");
Console.WriteLine("");
int x = 25;
int y = 10;
Console.Write("Is x equals to y?");
Console.WriteLine(x == y);
Console.Write("Is x less than y?");
Console.WriteLine(x < y);
Console.Write("Is x less than or equals to y?");
Console.WriteLine(x <= y);
Console.Write("Is x greater than y?");
Console.WriteLine(x > y);
Console.Write("Is x greater than or equals to y?");
Console.WriteLine(x >= y);
Console.Write("Is x not equal to y?");
Console.WriteLine(x != y);
}
}
Logical operators
Logical operators evaluate the logical operations between two boolean operands. The key thing to note here is "boolean operands". This means that the operands must be a boolean type or boolean expression.
Operator | Name | Description | Example |
---|---|---|---|
&& | Logical AND | It returns true only when both the operands evaluates to true; false otherwise. | x && y |
|| | Logical OR | It returns true only when either of operands evaluates to true; false otherwise. | x || y |
|| | Logical NOT | Its a unary operator. It returns true when the operand evaluates to false; false otherwise. | !x |
using System;
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Logical Operators");
Console.WriteLine("");
bool a = true;
bool b = false;
Console.Write("a = ");
Console.Write(a);
Console.Write(", b = ");
Console.WriteLine(b);
Console.WriteLine("");
Console.Write("a && b = ");
Console.WriteLine(a && b);
Console.WriteLine("");
Console.Write("a || b = ");
Console.WriteLine(a || b);
Console.WriteLine("");
Console.Write("!a = ");
Console.WriteLine(!a);
Console.WriteLine("");
}
}
Bitwise operators
These operators evaluates the bitwise operation between two numeric operands.
Operator | Name | Description | Example |
---|---|---|---|
& | Bitwise AND | Evaluates the bitwise AND between operands. | x & y |
| | Bitwise OR | Evaluates the bitwise OR between operands. | x | y |
^ | Bitwise XOR | Evaluates the bitwise XOR between operands. | x ^ y |
~ | One's Complement | It's a unary operator and evaluates the one's complement binary number or equivalent binary number of it's operand. | ~x |
Consider the below example.
using System;
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Bitwise Operators");
Console.WriteLine("");
int a = 1; // binary equivalent is 1
int b = 2; // binary equivalent is 10
Console.Write("a = ");
Console.Write(a);
Console.Write(", b = ");
Console.WriteLine(b);
Console.WriteLine("");
Console.Write("Bitwise AND : a & b = ");
Console.WriteLine(a & b);
Console.WriteLine("");
Console.Write("Bitwise OR : a | b = ");
Console.WriteLine(a | b);
Console.WriteLine("");
Console.Write("Bitwise XOR : a ^ b = ");
Console.WriteLine(a ^ b);
Console.WriteLine("");
Console.Write("One's complement : ~a = ");
Console.WriteLine(~a);
Console.WriteLine("");
}
}
Let's understand the above example. In this example, we have taken a = 1 and b = 2 are integers in decimal number system.
a's binary equivalent is 01 and b's binary equivalent is 10.
Now ANDing their bits from right to left will yield 00 as result.
[a] 01
[b] 10
[ANDing] --
00
Similarly, a | b will be evaluated as 01 | 10. Now ORing their bits from right to let will yield 11. The equivalent of 11 in decimal number system is 3 which would be shown as result.
[a] 01
[b] 10
[ORing] --
11
In the same way, the ^ operation will be performed.
The one's complement will invert all bits and yield the result. Since this negates the bits, so the number is represented in negative. Since this is signed binary integer, hence the system evaluates two's complement which is one's complement plus 1. Hence, one's complement for 1 is 0 and adding 1 to the most significant place will make it 10 which is 2 in decimal number system and with sign it will yield -2 as result.
Shift operators
These operator manipulates the bits of binary number or binary equivalent of a number and shifts either left or right.
Operator | Name | Description | Example |
---|---|---|---|
>> | Right Shift | It shifts the bits of a binary number or equivalent binary number of first operand n times to the right where n is the second operand. | x >> y |
<< | Left Shift | It shifts the bits of a binary number or equivalent binary number of first operand n times to the left where n is the second operand. | x << y |
Consider the below example
using System;
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Shift Operators");
Console.WriteLine("");
int a = 1; // binary equivalent is 1
int b = 2; // binary equivalent is 10
Console.Write("a = ");
Console.Write(a);
Console.Write(", b = ");
Console.WriteLine(b);
Console.WriteLine("");
Console.Write("Right shift : a >> b = ");
Console.WriteLine(a >> b);
Console.WriteLine("");
Console.Write("Left shift : a << b = ");
Console.WriteLine(a << b);
Console.WriteLine("");
}
}
The right shift operator >> shift the bits to the right. So, a >> b will shift the bits of 1 to 2 places to the right. Consider 1 as 001. Shifting one bit to the right will give 00 and shifting one more time to the right will give 0 as result which is 0 in decimal number system. The left shift a << b will shift the bits of 1 to 2 place to the left. Consider 1 as 001. Shifting one bit to right will give 010 and shifting one more time to the right will give 100 as result which is 4 in decimal number system.
[a] 00|01|00
[shift 2 places to right] 00|00|01
[result] 0000
[a] 00|01|00
[shift 2 places to left] 01|00|00
[result] 0100
* We always consider most significant digits
The Conditional operator
The conditional operator :? is the only ternary operator in C#. It is used to skip the simple if else block of statement (if-else statement will be covered later in this course).
It takes 3 operand and the format is - operand1 ? operand2 : operand3;
The operand1 must be a boolean operand or boolean expression. If the operand1 is true then it will return the operand2 otherwise it will return operand3 as result.
using System;
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Conditional Operator");
Console.WriteLine("");
int a = 1;
int b = 2;
string c = a <= b ? "a is less than or equals to b" : "a is greater than b";
Console.WriteLine(c);
}
}