Introduction
Operators are the core of any programming language, and they are defined as symbols that assist the programmer in performing certain mathematical and logical operations on operands. The operator '*', for example, is used for multiplication, as z= x * y;
The operator '*' is the multiplication operator, and the operands 'x' and 'y' are operands. The multiplication operator instructs the compiler to multiply both operands and store the result in the variable 'z.'
Without operators, any programming language is incomplete.
Recommended Topic, Palindrome in C# and Ienumerable vs Iqueryable.
Types of operators
In C#, there are many types of operators which can be classified into the following:
Arithmetic operators
Arithmetic Operators are used for performing mathematical/arithmetic operations on the operands. Arithmetic operators are of the following types:
In the below image, we can see all the arithmetic operators with their descriptions.

Unary operators
Unary operators are operators that work with a single operand. For example: Increment operator (++) and decrement operator (--)
E.g.
int val=5;
++val: // val=6
Binary operators
Binary operators are operators that work with two operands. For example: Division(/),Multiplication(*),Addition (+), Subtraction(-) .
E.g.:
int x=1;
int y=2;
int z=y-x; // 1
Ternary Operators
The conditional operator, also known as the ternary operator, is a decision-making operator in C# which takes three operands. It is the abbreviation for the if-else conditions.
A boolean condition is used to start the ternary operator. If this condition is true, the first statement after? will be executed; otherwise, the second statement after: will be executed.
Syntax:
condition ? statement 1 : statement 2 |
Example:
int x = 20, y = 10;
var result = x > y ? "x is greater than y" : "x is less than y";
C# program to show the working of Arithmetic Operators:
using System;
namespace Arithmetic
{
class CN
{
// Main Function
static void Main(string[] args)
{
int result;
int x = 12, y = 6;
// Addition
result = (x + y);
Console.WriteLine("x + y = " + result);
// Subtraction
result = (x - y);
Console.WriteLine("x - y = " + result);
// Multiplication
result = (x * y);
Console.WriteLine("x * y = " + result);
// Division
result = (x / y);
Console.WriteLine("x / y = " + result);
// Modulo
result = (x % y);
Console.WriteLine("x % y = " + result);
// Increment
x++;
Console.WriteLine("x++ = " + x);
// Decremental
y--;
Console.WriteLine("y-- = " + y);
}
}
}
Output:
x + y = 18 x - y = 6 x * y = 72 x / y = 2 x % y = 0 x++ = 13 Y- - = 5 |
Relational operators
Relational operators are used to comparing the two operands' values and form a relationship between them. For example, If we determine whether one operand is greater than the other, or whether two operands are equal, and so on.

C# program to show the working of Relational Operators:
using System;
namespace Relational {
class CN {
// Main Function
static void Main(string[] args)
{
bool result;
int x = 2, y = 10;
// Equal to Operator
result = (x == y);
Console.WriteLine("x == y gives " + result);
// Greater than Operator
result = (x > y);
Console.WriteLine("x > y gives " + result);
// Less than Operator
result = (x < y);
Console.WriteLine("x < y gives " + result);
// Greater than Equal to Operator
result = (x >= y);
Console.WriteLine("x >= y gives "+ result);
// Less than Equal to Operator
result = (x <= y);
Console.WriteLine("x <= y gives "+ result);
// Not Equal To Operator
result = (x != y);
Console.WriteLine("x != y gives " + result);
}
}
}
Output:
x == y gives False x > y gives False x < y gives True x >= y gives False x <= y gives True x != y gives True |
Logical operators
Logical operators such as "and" and "or" are used to execute logical operations. Logical operators return boolean values based on boolean expressions (true and false). In decision-making and loops, logical operators are used.
For example: (2<6) && (2!=7) ;// true

C# program to show the working of Logical Operators:
using System;
namespace Logical {
class CN {
// Main Function
static void Main(string[] args)
{
bool a = true,b = false, result;
// AND operator
result = a && b;
Console.WriteLine("a && b is " + result);
// OR operator
result = a || b;
Console.WriteLine("a || b is "+ result);
// NOT operator
result = !a;
Console.WriteLine("!a is " + result);
}
}
}
Output:
a && b is False a || b is True !a is False |
Bitwise operators
On the operands, these operators are used to perform bit-level operations.
The operators are converted to bit-level first, and then operations on the operands are performed. If arithmetic operations are performed at the bit level, the results can be obtained faster. They only operate with integral data types; they don't work with floating-point numbers.
For example
int a=5; // a = 00000101
-a; // a=11111010

C# program to show the working of Bitwise Operators:
using System;
namespace Bitwise {
class CN {
// Main Function
static void Main(string[] args)
{
int x = 2, y = 3, result;
// Bitwise AND Operator
result = x & y;
Console.WriteLine("x & y: " + result);
// Bitwise OR Operator
result = x | y;
Console.WriteLine("x | y: " + result);
// Bitwise XOR Operator
result = x ^ y;
Console.WriteLine("x ^ y: " + result);
// Bitwise AND Operator
result = ~x;
Console.WriteLine("~x " + result);
// Bitwise LEFT SHIFT Operator
result = x << 2;
Console.WriteLine("x << 2: " + result);
// Bitwise RIGHT SHIFT Operator
result = x >> 2;
Console.WriteLine("x >> 2: " + result);
}
}
}
Output:
x & y: 2 x | y: 3 x ^ y: 1 ~x -3 x << 2: 8 x >> 2: 0 |
Assignment operators
These are used to assign values to variables by placing the variable on the left side of the operator and the value on the right side of the operator. Otherwise, the compiler will throw an error if the value on the right side of the operator is not of the same data type as the variable on the left side of the operator.

C# program to show the working of Assignment Operators:
using System;
namespace Assignment {
class CN {
// Main Function
static void Main(string[] args)
{
int x = 10;
// it means x = x + 5
x += 5;
Console.WriteLine(x);
// it means x = x - 2
x -= 2;
Console.WriteLine(x);
// it means x = x * 4
x *= 4;
Console.WriteLine(x);
// it means x = x / 4
x /= 4;
Console.WriteLine(x);
}
}
}
Output:
15 13 52 13 |
Conditional operator
- In this, an expression/condition is first evaluated and then acts based on the evaluation result.
- Syntax: Condition? Expression-1? Expression-2;
- In this first, we will evaluate the condition given, if the condition is true, then Expression-1 is executed, and if the condition is false, then Expression-2 is executed.
- We can replace if..else statements with these conditional operators.
C# program to demonstrate the working of Conditional Operator:
using System;
namespace Conditional {
class CN {
// Main Function
static void Main(string[] args)
{
int x = 15, y = 20, result;
// To find which is maximum among two numbers
result = x > y ? x : y;
Console.WriteLine("Max of x and y is : " + result);
}
}
}
Output:
Max of x and y is : 20 |
Read about Bitwise Operators in C here.