Lession - 7 Operators in C#
Lession - 7 Operators in C#
Now we are going to discuss Operators in C# with Examples. The Operators are the foundation of any
programming language. Thus, the functionality of any programming language is incomplete without
the use of operators. At the end of this session, you will understand what are Operators and when,
and how to use them in C# Application Development.
In C#, the Operators can also be categorized based on the Number of Operands:
1. Unary Operator: The Operator that takes one operand to perform the operation.
2. Binary Operator: Then Operator that takes two operands to perform the operation.
3. Ternary Operator: The Operator that takes three operands to perform the operation.
Arithmetic Operators in C#
The Arithmetic Operators in C# are used to perform arithmetic/mathematical operations like addition,
subtraction, multiplication, etc. on operands. The following Operators are falling in this category are:
// Addition Operation
Result = (Num1 + Num2);
Console.WriteLine("Addition Operator: " + Result);
// Subtraction Operation
Result = (Num1 - Num2);
Console.WriteLine("Subtraction Operator: " + Result);
// Multiplication Operation
Result = (Num1 * Num2);
Console.WriteLine("Multiplication Operator: " + Result);
// Division Operation
Result = (Num1 / Num2);
Console.WriteLine("Division Operator: " + Result);
// Modulo Operation
Result = (Num1 % Num2);
Console.WriteLine("Modulo Operator: " + Result);
Console.ReadKey();
}
}
}
Output:
The most important point that you need to keep in mind is that the value on the right side must be of
the same data type as the variable on the left side else we will get a compile-time error. The different
Types of Assignment Operators supported in the C# language are as follows:
// it means x = x + 10
x += 10;
Console.WriteLine("Add Assignment Operator: " + x);
// it means x = x - 5
x -= 5;
Console.WriteLine("Subtract Assignment Operator: " + x);
// it means x = x * 5
x *= 5;
Console.WriteLine("Multiply Assignment Operator: " + x);
// it means x = x / 5
x /= 5;
Console.WriteLine("Division Assignment Operator: " + x);
// it means x = x % 5
x %= 5;
Console.WriteLine("Modulo Assignment Operator: " + x);
Console.ReadKey();
}
}
}
Output:
Equal to (==):
This Operator is used to return true if the left-hand side is equal to the right-hand side. For example,
5==3 is evaluated to be false. So, this Equal to (==) operator will check whether the two given
operands are equal or not. If equal returns true else returns false.
// Equal to Operator
Result = (Num1 == Num2);
Console.WriteLine("Equal (=) to Operator: " + Result);
Console.ReadKey();
}
}
}
Output:
Logical OR (||):
This operator is used to return true if either of the Boolean expressions is true. For example, false ||
true is evaluated to be true. That means the Logical OR (||) operator returns true when one (or both)
of the conditions in the expression is satisfied. Otherwise, it will return false. For example, a || b
returns true if either a or b is true. Also, it returns true when both a and b are true.
//Logical OR operator
z = x || y;
Console.WriteLine("Logical OR Operator (||) : " + z);
Console.ReadKey();
}
}
}
Output:
Bitwise OR (|)
Bitwise OR operator is represented by |. This operator performs the bitwise OR operation on the
corresponding bits of the two operands involved in the operation. If one of the bits or both bits are 1, it
gives 1. If not, it gives 0.
For example,
int a=12, b=25;
int result = a|b; //29
How?
12 Binary Number: 00001100
25 Binary Number: 00011001
Bitwise OR operation between 12 and 25:
00001100
00011001
========
00011101 (it is 29 in decimal)
Note: If the operands are of type bool, the bitwise OR operation is equivalent to the logical OR
operation between them.
For example,
int a=12, b=25;
int result = a&b; //8
How?
12 Binary Number: 00001100
25 Binary Number: 00011001
Bitwise AND operation between 12 and 25:
00001100
00011001
========
00001000 (it is 8 in decimal)
Note: If the operands are of type bool, the bitwise AND operation is equivalent to the logical AND
operation between them.
For example,
int a=12, b=25;
int result = a^b; //21
How?
12 Binary Number: 00001100
25 Binary Number: 00011001
// Bitwise OR Operator
Result = a | b;
Console.WriteLine("Bitwise OR: " + Result);
Console.ReadKey();
}
}
}
Output:
Pre-Increment Operators:
The Pre-Increment Operators are the operators which is used as a prefix to its variable. It is placed
before the variable. For example, ++a will increase the value of the variable a by 1.
Syntax: ++Variable;
Example: ++x;
Pre-Decrement Operators:
The Pre-Decrement Operators are the operators that are a prefix to its variable. It is placed before the
variable. For example, --a will decrease the value of the variable a by 1.
Syntax: --Variable;
Example: --x;
Note: Increment Operator means to increment the value of the variable by 1 and Decrement Operator
means to decrement the value of the variable by 1.
// Pre-Increment
int y = 10;
int Result2 = ++y;
//y and Result2 have same values = 11
Console.WriteLine("y is {0} and Result2 is {1}", y, Result2);
Console.ReadKey();
}
}
}
Output:
Note: The ++ operator in C# is used to increment the value of an integer. When placed before the
variable name called pre-increment operator), its value is incremented instantly. For example, ++x.
And when it is placed after the variable name 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++.
// Pre-Decrement
int y = 10;
int Result2 = --y;
//y and Result2 have same values i.e. 9
Console.WriteLine("y is {0} and Result2 is {1}", y, Result2);
Console.ReadKey();
}
}
}
Output:
Note: The -- operator in C# is used to decrement the value of an integer. When placed before the
variable name called pre-decrement operator), its value is decremented instantly. For example, --x.
And when it is placed after the variable name 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–.
The above statement means that first, we need to evaluate the condition. If the condition is true the
first_expression is executed and becomes the result and if the condition is false, the
second_expression is executed and becomes the result.
Console.ReadKey();
}
}
}
Output: