Introduction

The operator is a special functionality that is used to manipulate mathematical equations. In C# or any Object-Oriented Programming language, a defined operator is the symbol that is used to perform a mathematical expression or some specific task. The operator always performs between two operands. This campaign aims to solve arithmetic equations.

For example, (a+b)^2, (a-b)^2, A^2 + B^2 + 2AB etc.

There are three types of operators in C#,

  1. Unary Operator
  2. Binary Operator
  3. Tannery operator

Unary Operator

Unary means single or one, which means that when an operation occurs with an operator, that operator is called a unary operator.

Suppose any operation is performed with a single operator that single operator is called a unary operator. A unary operator is used to increment or decrement the original value of a variable.

A single operation is performed with a single operator that single operator is called a unary operator. A unary operator is used to increment or decrement the original value of a variable.

There are two types of unary operators

For example, to determine the working of increment or decrement.

static void Main(string[] args) {
    int value1 = 56;
    Console.WriteLine("This is Example for Pre and Post Increment Operator");
    Console.WriteLine("Original Value: {0} ", value1); //print the original value.
    Console.WriteLine("Pre- Increment value: {0} ", ++value1); //First to print the value then increment operator perform.
    Console.WriteLine("Post-Increment Value: {0} ", value1++); // First to Increment the value then print the incremented value.
    Console.WriteLine("Original Value: {0} ", value1);
    Console.WriteLine("***************************************");
    Console.WriteLine("This is Example for Pre and Post Decrement Operator");
    Console.WriteLine("Original Value: {0} ", value1); //print the original value.
    Console.WriteLine("Pre-Decrement value: {0} ", --value1); //First to print the value then decrement operator perform.
    Console.WriteLine("Post-Decrement Value: {0} ", value1--); // First to decrement the value then print the decremented value.
    Console.WriteLine("Original Value: {0} ", value1);
    Console.ReadLine();
}

Output

Binary operator

Binary operators act upon two operands to produce a new value. Such, operators can be classified into different categories these are.

Arithmetic operators

The arithmetic operators are used in solving mathematical equations.

Example of Binary operator

static void Main(string[] args) {
    int value1 = 55;
    int value2 = 25;
    Console.WriteLine("This is Example of Binay Operator");
    Console.WriteLine("Arithmetic Operation with two operands Value1 = {0}, value2 = {1} ", value1, value2);
    Console.WriteLine("Addition of two Number: {0} ", value1 + value2);
    Console.WriteLine("Subtraction of two Number: {0} ", value1 - value2);
    Console.WriteLine("Multiplication of two Number: {0} ", value1 * value2);
    Console.WriteLine("Division of two Number: {0} ", value1 / value2);
    Console.WriteLine("Modulo of two Number: {0} ", value1 % value2);
    Console.ReadLine();
}

Output

Relational Operator

Relational operators are used to compare two different things.

Example of Relational Operators

static void Main(string[] args) {
    int value1 = 55;
    int value2 = 60;
    Console.WriteLine("This is Example of Relational Operator");
    Console.WriteLine("Relational Operator(==, <=, >=, <, >, !=)");
    Console.WriteLine("value1 = {0} value2 = {1}", value1, value2);
    if (value1 == value2) {
        Console.WriteLine("Value is equal");
    } else {
        Console.WriteLine("Value is not equal");
    }
    if (value1 <= value2) {
        Console.WriteLine("{0} is Less", value1);
    } else {
        Console.WriteLine("{0} is greater ", value2);
    }
    if (value1 >= value2) {
        Console.WriteLine("{0} is Less", value1);
    } else {
        Console.WriteLine("{0} is greater ", value2);
    }
    Console.ReadLine();
}

Output

Logical Operator

logical operators are performed in logical operations with binary numbers.

This is the example of logical operator.

static void Main(string[] args) {
    Console.WriteLine("This is the example of Logical Operator");
    int value1 = 80, value2 = 75, value3 = 60;
    if (value1 >= value2 && value1 >= value3) {
        Console.WriteLine("{0} is the largest number ", value1);
    } else if (value2 >= value1 && value2 >= value3) {
        Console.WriteLine("{0} is the largest number ", value2);
    } else if (value3 >= value1 && value3 >= value2) {
        Console.WriteLine("{0} is the largest number ", value3);
    }
    Console.ReadLine();
}

Output

Assignment Operator

The assignment operator is used to assign the value of the variables.

For example, int a=5, b=10, result=0;

static void Main(string[] args) {
    Console.WriteLine("This is the example of Assignment Operator");
    int value1 = 50;
    Console.WriteLine("original value1= {0} ", value1);
    value1 = 60; //Assignment operator used for assign a new value
    Console.WriteLine("After apply assignment operator value1= {0} ", value1);
    value1 = 80; //Assignment operator used for assign a new value
    Console.WriteLine("again apply assignment operator aalue1= {0} ", value1);
    Console.ReadLine();
}

Output

Bitwise Operator

The bitwise operator is work for binary operations.

Tannery Operator or Conditional Operator

The tannery operator is used to define conditional statements in a single line.

For example, C = (a > b)? a : b

static void Main(string[] args) {
    Console.WriteLine("This is the example of Tannery Operator");
    int value1 = 80;
    int value2 = 60;
    int result = value1 < value2 ? value1 : value2;
    Console.WriteLine("{0} is less ", result);
    Console.ReadLine();
}

Output

Key Points