Operators in C#

Operators in C#


Unary Operator - Unary operators take only one operand for operation. Either they use prefix or postfix notaion.

Binary Operator - Binary operators take only two operand for operation

Ternary Operator - Ternary operator take three operands for operation. In C# only one ternary operator (?) exists.

1. Arithmetic Operators(+, -, /, %)

Arithmetic operators are work with integers(int, long, uint, ulong).

  • + operator used for addition
  • - operator used for substraction or negation
  • * operator used for multiplication
  • / operator used for remainder of division
// Arithmetic Operators Examples

int a = 10;
int b = 2; 
int sum = a + b; // 12 
int substract = a - b; // 8 
int multiply = a * b;  // 20 
int divide = a / b; // 5 
int reminder = a % b;  // 0

2. Relational Operators(==, !=, >, >=, <, <=)

// Relational Operators Examples

int a = 10;
int b = 2; 

bool obj1 = a == b; // false 
bool obj2 = a != b; // true 
bool obj3 = a > b;  // true 
bool obj4 = a >= b; // true 
bool obj5 = a < b; // false 
bool obj6 = a <= b;  // false

3. Shift Operators(<<,>>)

Left Shift Operator <<

Left shift operator shift the bit to left side. It simply add bit in right side and removed from left side. If you want to moved any thing to right side then you have to push some force from left side towards right side.

// Shift Operators Examples

int a = 5; // 0101
int b = 2; // 0010
int c = 3; // 0011
 
// Left Shift Operators
int A = a << 1;  // 0101  1010 10
int B = b << 1;  // 0010  0100  4
int C = c << 1;  // 0011  0110 6
Right Shift operator >>

Right shift operator shift the bit to right side. It simply add bit in left side and removed from right side. If you want to moved any thing to left side then you have to push some force from right side towards left side.

// Shift Operators Examples

int a = 5; // 0101
int b = 2; // 0010
int c = 3; // 0011 

// Right Shift Operators
int A1 = a >> 1;  // 0101  0010 2
int B1 = b >> 1;  // 0010  0001 1
int C1 = c >> 1;   // 0011  0001 1

4. Logical Operators

&, |, ~, ^

// & Operator

// true & true   => true
// true & false  => false
// false & true => false
// false & false => false 

int a = 5;
int b = 10;
if (a == b & a < b)
{
Console.WriteLine("AND operator is satisfied");
}
else
{
Console.WriteLine("AND operator is not satisfied");
}
// Output:
// AND operator is not satisfied

// | Operator

// true | true   => true
// true | false  => true
// false | true => true
// false | false => false 

int a = 5;
int b = 10;
if (a == b | a < b)
{
    Console.WriteLine("OR operator is satisfied");
}
else
{
    Console.WriteLine("OR operator is not satisfied");
}
// Output:
// OR operator is satisfied

^ Operator


// XOR Operator

// true & true   => false
// true & false  => true
// false & true => true
// false & false => false

~ is complementry operator

5. Bool Logic Operators

&&, ||

&& Operator

&& operator is same as & operator but only difference that, if && operator found first condition is return false then it will not execute another conditions.

|| Operator

&& operator is same as & operator but only difference that, if || operator found first condition is return true then it will not execute another conditions.

6. Conditional Operator

?

condition ? (run if true) : (run if false)

// Conditional Operator Syntax
// <condition> ? <true expression> : <false expression>

int x = 5; 
int y = x > 1 ? x : 10; 

// Output
// y = 5

7. Null-Coalescing Operator(??)

?? Operator of null checking. Mainly used for nullable types.

Left part of ?? is condition, if that condition evaluated to null then only its executing right side part of ??.

int? count = null;
int result = count ?? 0;
Console.WriteLine(result); 

// Output
// result = 0;

?? Operator can use for any type of null value checking.

string name = null;
string result1 = name ?? "ProgramIdea";
Console.WriteLine(result1);

// Output
// result = ProgramIdea