Operators are the foundation of any programming language. Thus the functionality of C# language is incomplete without the use of operators. Operators allow us to perform different kinds of operations on operands. In C#, operators Can be categorized based upon their different functionality :
In C#, Operators can also categorized based upon Number of Operands :
- Unary Operator: Operator that takes one operand to perform the operation.
- Binary Operator: Operator that takes two operands to perform the operation.
- Ternary Operator: Operator that takes three operands to perform the operation.
Arithmetic Operators
These are used to perform arithmetic/mathematical operations on operands. The Binary Operators falling in this category are :
- Addition: The ‘+’ operator adds two operands. For example, x+y.
- Subtraction: The ‘-‘ operator subtracts two operands. For example, x-y.
- Multiplication: The ‘*’ operator multiplies two operands. For example, x*y.
- Division: The ‘/’ operator divides the first operand by the second. For example, x/y.
- Modulus: The ‘%’ operator returns the remainder when first operand is divided by the second. For example, x%y.
Example:
C#
using System;
namespace Arithmetic
{
class GFG
{
static void Main( string [] args)
{
int result;
int x = 10, y = 5;
result = (x + y);
Console.WriteLine( "Addition Operator: " + result);
result = (x - y);
Console.WriteLine( "Subtraction Operator: " + result);
result = (x * y);
Console.WriteLine( "Multiplication Operator: " + result);
result = (x / y);
Console.WriteLine( "Division Operator: " + result);
result = (x % y);
Console.WriteLine( "Modulo Operator: " + result);
}
}
}
|
Output:
Addition Operator: 15
Subtraction Operator: 5
Multiplication Operator: 50
Division Operator: 2
Modulo Operator: 0
The ones falling into the category of Unary Operators are:
- Increment: The ‘++’ operator is used to increment the value of an integer. When placed before the variable name (also called pre-increment operator), its value is incremented instantly. For example, ++x.
And when it is placed after the variable name (also called post-increment operator), its value is preserved temporarily until the execution of this statement and it gets updated before the execution of the next statement. For example, x++. - Decrement: The ‘- -‘ operator is used to decrement the value of an integer. When placed before the variable name (also called pre-decrement operator), its value is decremented instantly. For example, – -x.
And when it is placed after the variable name (also called post-decrement operator), its value is preserved temporarily until the execution of this statement and it gets updated before the execution of the next statement. For example, x- –.
Example:
C#
using System;
namespace Arithmetic {
class GFG {
static void Main( string [] args)
{
int a = 10, res;
res = a++;
Console.WriteLine( "a is {0} and res is {1}" , a, res);
res = a--;
Console.WriteLine( "a is {0} and res is {1}" , a, res);
res = ++a;
Console.WriteLine( "a is {0} and res is {1}" , a, res);
res = --a;
Console.WriteLine( "a is {0} and res is {1}" ,a, res);
}
}
}
|
Output:
a is 11 and res is 10
a is 10 and res is 11
a is 11 and res is 11
a is 10 and res is 10
Relational Operators
Relational operators are used for comparison of two values. Let’s see them one by one:
- ‘=='(Equal To) operator checks whether the two given operands are equal or not. If so, it returns true. Otherwise it returns false. For example, 5==5 will return true.
- ‘!='(Not Equal To) operator checks whether the two given operands are equal or not. If not, it returns true. Otherwise it returns false. It is the exact boolean complement of the ‘==’ operator. For example, 5!=5 will return false.
- ‘>'(Greater Than) operator checks whether the first operand is greater than the second operand. If so, it returns true. Otherwise it returns false. For example, 6>5 will return true.
- ‘<‘(Less Than) operator checks whether the first operand is lesser than the second operand. If so, it returns true. Otherwise it returns false. For example, 6<5 will return false.
- ‘>='(Greater Than Equal To) operator checks whether the first operand is greater than or equal to the second operand. If so, it returns true. Otherwise it returns false. For example, 5>=5 will return true.
- ‘<='(Less Than Equal To) operator checks whether the first operand is lesser than or equal to the second operand. If so, it returns true. Otherwise it returns false. For example, 5<=5 will also return true.
Example:
C#
using System;
namespace Relational {
class GFG {
static void Main( string [] args)
{
bool result;
int x = 5, y = 10;
result = (x == y);
Console.WriteLine( "Equal to Operator: " + result);
result = (x > y);
Console.WriteLine( "Greater than Operator: " + result);
result = (x < y);
Console.WriteLine( "Less than Operator: " + result);
result = (x >= y);
Console.WriteLine( "Greater than or Equal to: " + result);
result = (x <= y);
Console.WriteLine( "Lesser than or Equal to: " + result);
result = (x != y);
Console.WriteLine( "Not Equal to Operator: " + result);
}
}
}
|
Output:
Equal to Operator: False
Greater than Operator: False
Less than Operator: True
Greater than or Equal to: False
Lesser than or Equal to: True
Not Equal to Operator: True
Logical Operators
They are used to combine two or more conditions/constraints or to complement the evaluation of the original condition in consideration. They are described below:
- Logical AND: The ‘&&’ operator returns true when both the conditions in consideration are satisfied. Otherwise it returns false. For example, a && b returns true when both a and b are true (i.e. non-zero).
- Logical OR: The ‘||’ operator returns true when one (or both) of the conditions in consideration is satisfied. Otherwise it returns false. For example, a || b returns true if one of a or b is true (i.e. non-zero). Of course, it returns true when both a and b are true.
- Logical NOT: The ‘!’ operator returns true the condition in consideration is not satisfied. Otherwise it returns false. For example, !a returns true if a is false, i.e. when a=0.
Example:
C#
using System;
namespace Logical {
class GFG {
static void Main( string [] args)
{
bool a = true ,b = false , result;
result = a && b;
Console.WriteLine( "AND Operator: " + result);
result = a || b;
Console.WriteLine( "OR Operator: " + result);
result = !a;
Console.WriteLine( "NOT Operator: " + result);
}
}
}
|
Output:
AND Operator: False
OR Operator: True
NOT Operator: False
Bitwise Operators
In C#, there are 6 bitwise operators which work at bit level or used to perform bit by bit operations. Following are the bitwise operators :
- & (bitwise AND) Takes two numbers as operands and does AND on every bit of two numbers. The result of AND is 1 only if both bits are 1.
- | (bitwise OR) Takes two numbers as operands and does OR on every bit of two numbers. The result of OR is 1 any of the two bits is 1.
- ^ (bitwise XOR) Takes two numbers as operands and does XOR on every bit of two numbers. The result of XOR is 1 if the two bits are different.
- << (left shift) Takes two numbers, left shifts the bits of the first operand, the second operand decides the number of places to shift.
- >> (right shift) Takes two numbers, right shifts the bits of the first operand, the second operand decides the number of places to shift.
Example:
C#
using System;
namespace Bitwise {
class GFG {
static void Main( string [] args)
{
int x = 5, y = 10, result;
result = x & y;
Console.WriteLine( "Bitwise AND: " + result);
result = x | y;
Console.WriteLine( "Bitwise OR: " + result);
result = x ^ y;
Console.WriteLine( "Bitwise XOR: " + result);
result = ~x;
Console.WriteLine( "Bitwise Complement: " + result);
result = x << 2;
Console.WriteLine( "Bitwise Left Shift: " + result);
result = x >> 2;
Console.WriteLine( "Bitwise Right Shift: " + result);
}
}
}
|
Output:
Bitwise AND: 0
Bitwise OR: 15
Bitwise XOR: 15
Bitwise Complement: -6
Bitwise Left Shift: 20
Bitwise Right Shift: 1
Assignment Operators
Assignment operators are used to assigning a value to a variable. The left side operand of the assignment operator is a variable and right side operand of the assignment operator is a value. The value on the right side must be of the same data-type of the variable on the left side otherwise the compiler will raise an error.
Different types of assignment operators are shown below:
- “=”(Simple Assignment): This is the simplest assignment operator. This operator is used to assign the value on the right to the variable on the left.
Example:
a = 10;
b = 20;
ch = 'y';
- “+=”(Add Assignment): This operator is combination of ‘+’ and ‘=’ operators. This operator first adds the current value of the variable on left to the value on the right and then assigns the result to the variable on the left.
Example:
(a += b) can be written as (a = a + b)
If initially value stored in a is 5. Then (a += 6) = 11.
- “-=”(Subtract Assignment): This operator is combination of ‘-‘ and ‘=’ operators. This operator first subtracts the current value of the variable on left from the value on the right and then assigns the result to the variable on the left.
Example:
(a -= b) can be written as (a = a - b)
If initially value stored in a is 8. Then (a -= 6) = 2.
- “*=”(Multiply Assignment): This operator is combination of ‘*’ and ‘=’ operators. This operator first multiplies the current value of the variable on left to the value on the right and then assigns the result to the variable on the left.
Example:
(a *= b) can be written as (a = a * b)
If initially value stored in a is 5. Then (a *= 6) = 30.
- “/=”(Division Assignment): This operator is combination of ‘/’ and ‘=’ operators. This operator first divides the current value of the variable on left by the value on the right and then assigns the result to the variable on the left.
Example:
(a /= b) can be written as (a = a / b)
If initially value stored in a is 6. Then (a /= 2) = 3.
- “%=”(Modulus Assignment): This operator is combination of ‘%’ and ‘=’ operators. This operator first modulo the current value of the variable on left by the value on the right and then assigns the result to the variable on the left.
Example:
(a %= b) can be written as (a = a % b)
If initially value stored in a is 6. Then (a %= 2) = 0.
- “<<=”(Left Shift Assignment) : This operator is combination of ‘<<‘ and ‘=’ operators. This operator first Left shift the current value of the variable on left by the value on the right and then assigns the result to the variable on the left.
Example:
(a <<= 2) can be written as (a = a << 2)
If initially value stored in a is 6. Then (a <<= 2) = 24.
- “>>=”(Right Shift Assignment) : This operator is combination of ‘>>’ and ‘=’ operators. This operator first Right shift the current value of the variable on left by the value on the right and then assigns the result to the variable on the left.
Example:
(a >>= 2) can be written as (a = a >> 2)
If initially value stored in a is 6. Then (a >>= 2) = 1.
- “&=”(Bitwise AND Assignment): This operator is combination of ‘&’ and ‘=’ operators. This operator first “Bitwise AND” the current value of the variable on the left by the value on the right and then assigns the result to the variable on the left.
Example:
(a &= 2) can be written as (a = a & 2)
If initially value stored in a is 6. Then (a &= 2) = 2.
- “^=”(Bitwise Exclusive OR): This operator is combination of ‘^’ and ‘=’ operators. This operator first “Bitwise Exclusive OR” the current value of the variable on left by the value on the right and then assigns the result to the variable on the left.
Example:
(a ^= 2) can be written as (a = a ^ 2)
If initially value stored in a is 6. Then (a ^= 2) = 4.
- “|=”(Bitwise Inclusive OR) : This operator is combination of ‘|’ and ‘=’ operators. This operator first “Bitwise Inclusive OR” the current value of the variable on left by the value on the right and then assigns the result to the variable on the left.
Example :
(a |= 2) can be written as (a = a | 2)
If initially, value stored in a is 6. Then (a |= 2) = 6.
Example:
C#
using System;
namespace Assignment {
class GFG {
static void Main( string [] args)
{
int x = 15;
x += 10;
Console.WriteLine( "Add Assignment Operator: " + x);
x = 20;
x -= 5;
Console.WriteLine( "Subtract Assignment Operator: " + x);
x = 15;
x *= 5;
Console.WriteLine( "Multiply Assignment Operator: " + x);
x = 25;
x /= 5;
Console.WriteLine( "Division Assignment Operator: " + x);
x = 25;
x %= 5;
Console.WriteLine( "Modulo Assignment Operator: " + x);
x = 8;
x <<= 2;
Console.WriteLine( "Left Shift Assignment Operator: " + x);
x = 8;
x >>= 2;
Console.WriteLine( "Right Shift Assignment Operator: " + x);
x = 12;
x &= 4;
Console.WriteLine( "Bitwise AND Assignment Operator: " + x);
x = 12;
x ^= 4;
Console.WriteLine( "Bitwise Exclusive OR Assignment Operator: " + x);
x = 12;
x |= 4;
Console.WriteLine( "Bitwise Inclusive OR Assignment Operator: " + x);
}
}
}
|
Output :
Add Assignment Operator: 25
Subtract Assignment Operator: 15
Multiply Assignment Operator: 75
Division Assignment Operator: 5
Modulo Assignment Operator: 0
Left Shift Assignment Operator: 32
Right Shift Assignment Operator: 2
Bitwise AND Assignment Operator: 4
Bitwise Exclusive OR Assignment Operator: 8
Bitwise Inclusive OR Assignment Operator: 12
Conditional Operator
It is ternary operator which is a shorthand version of if-else statement. It has three operands and hence the name ternary. It will return one of two values depending on the value of a Boolean expression.
Syntax:
condition ? first_expression : second_expression;
Explanation:
condition: It must be evaluated to true or false.
If the condition is true
first_expression is evaluated and becomes the result.
If the condition is false,
second_expression is evaluated and becomes the result.
Example:
C#
using System;
namespace Conditional {
class GFG {
static void Main( string [] args)
{
int x = 5, y = 10, result;
result = x > y ? x : y;
Console.WriteLine( "Result: " + result);
result = x < y ? x : y;
Console.WriteLine( "Result: " + result);
}
}
}
|
Output :
Result: 10
Result: 5