Introduction
The operator is a special functionality that is used to manipulate mathematical equations. In C# or any Object-Oriented Programming language, a defined operator is the symbol that is used to perform a mathematical expression or some specific task. The operator always performs between two operands. This campaign aims to solve arithmetic equations.
For example, (a+b)^2, (a-b)^2, A^2 + B^2 + 2AB etc.
There are three types of operators in C#,
- Unary Operator
- Binary Operator
- Tannery operator
Unary Operator
Unary means single or one, which means that when an operation occurs with an operator, that operator is called a unary operator.
Suppose any operation is performed with a single operator that single operator is called a unary operator. A unary operator is used to increment or decrement the original value of a variable.
A single operation is performed with a single operator that single operator is called a unary operator. A unary operator is used to increment or decrement the original value of a variable.
There are two types of unary operators
- Increment
It raises the value of the variable increment operator, which is commonly used in loops.
- Pre-increment
To increment first then print the value.
- Post-increment
To first print the value then increment.
- Decrement
It decries the value of the variable decrement operator, which is commonly used in loops.
- Pre-decrement
To decrement the first then print the value.
- Post-decrement
To first print the value then decrement.
For example, to determine the working of increment or decrement.
static void Main(string[] args) {
int value1 = 56;
Console.WriteLine("This is Example for Pre and Post Increment Operator");
Console.WriteLine("Original Value: {0} ", value1); //print the original value.
Console.WriteLine("Pre- Increment value: {0} ", ++value1); //First to print the value then increment operator perform.
Console.WriteLine("Post-Increment Value: {0} ", value1++); // First to Increment the value then print the incremented value.
Console.WriteLine("Original Value: {0} ", value1);
Console.WriteLine("***************************************");
Console.WriteLine("This is Example for Pre and Post Decrement Operator");
Console.WriteLine("Original Value: {0} ", value1); //print the original value.
Console.WriteLine("Pre-Decrement value: {0} ", --value1); //First to print the value then decrement operator perform.
Console.WriteLine("Post-Decrement Value: {0} ", value1--); // First to decrement the value then print the decremented value.
Console.WriteLine("Original Value: {0} ", value1);
Console.ReadLine();
}
Output
Binary operator
Binary operators act upon two operands to produce a new value. Such, operators can be classified into different categories these are.
Arithmetic operators
The arithmetic operators are used in solving mathematical equations.
- Addition(+)
The addition operator is used to sum two numbers. For example a+b
- Subtraction (-)
The subtraction operator is used to subtract two numbers. For example a-b
- Multiplication (*)
The multiplication operator is used to multiply two numbers. For example a*b
- Division (/)
The division operator is used to divide two numbers. For example a/b
- Modulo (%)
The modulo operator is used to modulo two numbers. For example a%b
Example of Binary operator
static void Main(string[] args) {
int value1 = 55;
int value2 = 25;
Console.WriteLine("This is Example of Binay Operator");
Console.WriteLine("Arithmetic Operation with two operands Value1 = {0}, value2 = {1} ", value1, value2);
Console.WriteLine("Addition of two Number: {0} ", value1 + value2);
Console.WriteLine("Subtraction of two Number: {0} ", value1 - value2);
Console.WriteLine("Multiplication of two Number: {0} ", value1 * value2);
Console.WriteLine("Division of two Number: {0} ", value1 / value2);
Console.WriteLine("Modulo of two Number: {0} ", value1 % value2);
Console.ReadLine();
}
Output
Relational Operator
Relational operators are used to compare two different things.
- Less than (<)
It is used to compare, which one is less. For example a<b.
- Greater than (>)
It is used to compare, which one is greater. For example a>b.
- Less than equal to (<=)
It is used to compare which one is less or equal. For example, a<=b
- Greater than equal to (>=)
It is used to compare which one is greater or equal. For example, a>=b
- Equal to (==)
It is used to compare whether both are equal or not. For example, a==b
- Not equal to (! =)
It is used to compare whether both are different or not. For example, a!=b
Example of Relational Operators
static void Main(string[] args) {
int value1 = 55;
int value2 = 60;
Console.WriteLine("This is Example of Relational Operator");
Console.WriteLine("Relational Operator(==, <=, >=, <, >, !=)");
Console.WriteLine("value1 = {0} value2 = {1}", value1, value2);
if (value1 == value2) {
Console.WriteLine("Value is equal");
} else {
Console.WriteLine("Value is not equal");
}
if (value1 <= value2) {
Console.WriteLine("{0} is Less", value1);
} else {
Console.WriteLine("{0} is greater ", value2);
}
if (value1 >= value2) {
Console.WriteLine("{0} is Less", value1);
} else {
Console.WriteLine("{0} is greater ", value2);
}
Console.ReadLine();
}
Output
Logical Operator
logical operators are performed in logical operations with binary numbers.
- OR (||)
It is used for more than one condition and if the single condition is true then execute the result otherwise the condition is false.
For example, if((a==5) || (a==6))
- NOT(!)
It is used to check condition is false or not.
For example if(a!=5)
- AND(&&)
It is used for more than one condition and if all condition is true then execute the result otherwise condition is false.
For example, if((username=="username") && (password=="password"))
This is the example of logical operator.
static void Main(string[] args) {
Console.WriteLine("This is the example of Logical Operator");
int value1 = 80, value2 = 75, value3 = 60;
if (value1 >= value2 && value1 >= value3) {
Console.WriteLine("{0} is the largest number ", value1);
} else if (value2 >= value1 && value2 >= value3) {
Console.WriteLine("{0} is the largest number ", value2);
} else if (value3 >= value1 && value3 >= value2) {
Console.WriteLine("{0} is the largest number ", value3);
}
Console.ReadLine();
}
Output
Assignment Operator
The assignment operator is used to assign the value of the variables.
For example, int a=5, b=10, result=0;
static void Main(string[] args) {
Console.WriteLine("This is the example of Assignment Operator");
int value1 = 50;
Console.WriteLine("original value1= {0} ", value1);
value1 = 60; //Assignment operator used for assign a new value
Console.WriteLine("After apply assignment operator value1= {0} ", value1);
value1 = 80; //Assignment operator used for assign a new value
Console.WriteLine("again apply assignment operator aalue1= {0} ", value1);
Console.ReadLine();
}
Output
Bitwise Operator
The bitwise operator is work for binary operations.
- Bitwise OR (|)
It is used for OR operation between two binary numbers.
- Bitwise Left Shift (<<)
It is used for left shift operation between two binary numbers.
- Bitwise Right Shift (>>)
It is used for right shift operation between two binary numbers.
- Bitwise XOR (^)
It is used for XOR operation between two binary numbers.
- Bitwise AND(&)
It is used for AND operation between two binary numbers.
Tannery Operator or Conditional Operator
The tannery operator is used to define conditional statements in a single line.
For example, C = (a > b)? a : b
static void Main(string[] args) {
Console.WriteLine("This is the example of Tannery Operator");
int value1 = 80;
int value2 = 60;
int result = value1 < value2 ? value1 : value2;
Console.WriteLine("{0} is less ", result);
Console.ReadLine();
}
Output
Key Points
- Operators can be used between two or more operands.
- Mathematical equations are always correct.
- The two operators are not used together.