An operator is a symbol that tells the compiler to perform specific mathematical or logical manipulations. C# has rich set of built-in operators and provides the following type of operators -
1. Basic assignment Operator
2. Arithmetic Operators
3. Relational Operators
4. Logical Operators
5. Unary Operators
6. Ternary Operators
7. Bitwise Operators
8. Miscellaneous Operators
The description of the above shown operators is as follows:
Basic assignment operator (=) is used to assign values to variables.
Example:
using System;
namespace Operator
{
class AssignmentOperator
{
public static void Main(string[] args)
{
int firstNumber, secondNumber;
// Assigning a constant to variable
firstNumber = 10;
Console.WriteLine("First Number = {0}", firstNumber);
// Assigning a variable to another variable
secondNumber = firstNumber;
Console.WriteLine("Second Number = {0}", secondNumber);
}
}
}
Arithmetic operators are used to perform arithmetic operations such as addition, subtraction, multiplication, division, etc.
The list of arithmetic operators is shown in the below table,
Operator |
Operator Name |
Example |
+ |
Addition Operator |
6 + 3 evaluates to 9 |
- |
Subtraction Operator |
10 - 6 evaluates to 4 |
* |
Multiplication Operator |
4 * 2 evaluates to 8 |
/ |
Division Operator |
10 / 5 evaluates to 2 |
% |
Modulo Operator (Remainder) |
16 % 3 evaluates to 1 |
Example:
using System;
namespace Operator{
class ArithmeticOperator{
public static void Main(string[] args)
{
double firstNumber = 14.40, secondNumber = 4.60, result;
int num1 = 26, num2 = 4, rem;
// Addition operator
result = firstNumber + secondNumber;
Console.WriteLine("{0} + {1} = {2}", firstNumber,
secondNumber, result);
// Subtraction operator
result = firstNumber - secondNumber;
Console.WriteLine("{0} - {1} = {2}", firstNumber,
secondNumber, result);
// Multiplication operator
result = firstNumber * secondNumber;
Console.WriteLine("{0} * {1} = {2}", firstNumber,
secondNumber, result);
// Division operator
result = firstNumber / secondNumber;
Console.WriteLine("{0} / {1} = {2}", firstNumber,
secondNumber, result);
// Modulo operator
rem = num1 % num2;
Console.WriteLine("{0} % {1} = {2}", num1, num2, rem);
}
}
}
Output: 14.4 + 4.6 = 19 14.4 - 4.6 = 9.8 14.4 * 4.6 = 66.24 14.4 / 4.6 = 3.130434782 26 % 4 = 2 |
Relational operators are used to check the relationship between two operands. If the relationship is true the result will be true, otherwise it will result in false.
Relational operators are used in decision making and loops.
The types of relational operators are as shown in the below table,
Operator |
Operator Name |
Example |
== |
Equal to |
6 == 4 evaluates to false |
> |
Greater than |
3 > -1 evaluates to true |
< |
Less than |
5 < 3 evaluates to false |
>= |
Greater than or equal to |
4 >= 4 evaluates to true |
<= |
Less than or equal to |
5 <= 3 evaluates to false |
!= |
Not equal to |
10 != 2 evaluates to true |
Example:
using System;
namespace Operator
{
class RelationalOperator
{
public static void Main(string[] args)
{
bool result;
int firstNumber = 10, secondNumber = 20;
result = (firstNumber==secondNumber);
Console.WriteLine("{0} == {1} returns {2}",firstNumber,
secondNumber, result);
result = (firstNumber > secondNumber);
Console.WriteLine("{0} > {1} returns {2}",firstNumber,
secondNumber, result);
result = (firstNumber < secondNumber);
Console.WriteLine("{0} < {1} returns {2}",firstNumber,
secondNumber, result);
result = (firstNumber >= secondNumber);
Console.WriteLine("{0} >= {1} returns {2}",firstNumber,
secondNumber, result);
result = (firstNumber <= secondNumber);
Console.WriteLine("{0} <= {1} returns {2}",firstNumber,
secondNumber, result);
result = (firstNumber != secondNumber);
Console.WriteLine("{0} != {1} returns {2}",firstNumber,
secondNumber, result);
}
}
}
Logical operators are used to perform logical operation such as AND, OR.
Logical operators operate on Boolean expressions (TRUE and FALSE) and return Boolean values.
Logical operators are used in Decision Making and Loops.
Here is how the result is evaluated for logical AND and OR operators,
Operand 1 |
Operand 2 |
OR (||) |
AND (&&) |
true |
true |
true |
true |
true |
false |
true |
false |
false |
true |
true |
false |
false |
false |
false |
false |
In simple words, the table can be summarized as:
Example:
using System;
namespace Operator
{
class LogicalOperator
{
public static void Main(string[] args)
{
bool result;
int firstNumber = 10, secondNumber = 20;
// OR operator
result = (firstNumber == secondNumber) || (firstNumber > 5);
Console.WriteLine(result);
// AND operator
result = (firstNumber == secondNumber) && (firstNumber > 5);
Console.WriteLine(result);
}
}
}
Output: True False |
The unary operators operate on a single operand.
The Unary operators and their description are given below:
Operator |
Operator Name |
Description |
+ |
Unary Plus |
Leaves the sign of operand as it is |
- |
Unary Minus |
Inverts the sign of operand |
++ |
Increment |
Increment value by 1 |
-- |
Decrement |
Decrement value by 1 |
! |
Logical Negation (Not) |
Inverts the value of a boolean |
Example:
using System;
namespace Operator
{
class UnaryOperator
{
public static void Main(string[] args)
{
int number = 10, result;
bool flag = true;
result = +number;
Console.WriteLine("+number = " + result);
result = -number;
Console.WriteLine("-number = " + result);
result = ++number;
Console.WriteLine("++number = " + result);
result = --number;
Console.WriteLine("--number = " + result);
Console.WriteLine("!flag = " + (!flag));
}
}
}
The ternary operator ? : operates on three operands. It is a shorthand for if-then-else statement.
Ternary operator can be used as follows:
Variable = Condition? Expression : Expression2; |
The ternary operator works as follows:
• If the expression stated by Condition is true, the result of Expression is assigned to variable.
• If it is false, the result of Expression2 is assigned to variable.
Example:
using System;
namespace Operator
{
class TernaryOperator
{
public static void Main(string[] args)
{
int number = 10;
string result;
result = (number % 2 == 0)? "Even Number" : "Odd
Number";
Console.WriteLine("{0} is {1}", number, result);
}
}
}
Output: 10 is Even number. |
Bitwise operator works on bits and perform bit by bit operation.
The description of the bitwise operators is as given in the table,
Operator |
Description |
Example |
& |
Binary AND Operator copies a bit to the result if it exists in both operands. |
(A & B) = 12, which is 0000 1100 |
| |
Binary OR Operator copies a bit if it exists in either operand. |
(A | B) = 61, which is 0011 1101 |
^ |
Binary XOR Operator copies the bit if it is set in one operand but not both. |
(A ^ B) = 49, which is 0011 0001 |
~ |
Binary Ones Complement Operator is unary and has the effect of 'flipping' bits. |
(~A ) = 61, which is 1100 0011 in 2's complement due to a signed binary number. |
<< |
Binary Left Shift Operator. The left operands value is moved left by the number of bits specified by the right operand. |
A << 2 = 240, which is 1111 0000 |
>> |
Binary Right Shift Operator. The left operands value is moved right by the number of bits specified by the right operand. |
A >> 2 = 15, which is 0000 1111 |
There are few other important operators including sizeof, typeof and ? : supported by C#.
The description of the miscellaneous operators with example is as shown in the below table,
Operator |
Description |
Example |
sizeof() |
Returns the size of a data type. |
sizeof(int), returns 4. |
typeof() |
Returns the type of a class. |
typeof(StreamReader); |
& |
Returns the address of an variable. |
&a; returns actual address of the variable. |
* |
Pointer to a variable. |
*a; creates pointer named 'a' to a variable. |
? : |
Conditional Expression |
If Condition is true ? Then value X : Otherwise value Y |
is |
Determines whether an object is of a certain type. |
If( Ford is Car) // checks if Ford is an object of the Car class. |
as |
Cast without raising an exception if the cast fails. |
Object obj = new StringReader("Hello"); StringReader r = obj as StringReader; |