C# - Operators


C# operators used to perform basic operations on variable and values. C# provides number of operators. Those operators include the following groups.

Arithmetic Operator

Perform arithmetic operations on numeric values.

A list of all arithmetic operators.

Operators Name Description Example
+ Addition Add two integer values x + y
- Subtraction Subtract one integer value from another integer value x - y
* Multiplication Multiply two values x * y
/ Division Divide one value by another x / y
% Modulus Returns the division remainder x % y
++ Increment Increment the value of variable by 1 X++
-- Decrement Decrement the value of variable by 1 X--
using System;

class Program
{
    static void Main()
    {
        // Declare and initialize variables
        int x = 10;
        int y = 5;

        // Addition
        int additionResult = x + y;
        Console.WriteLine($"Addition Result: {additionResult}");

        // Subtraction
        int subtractionResult = x - y;
        Console.WriteLine($"Subtraction Result: {subtractionResult}");

        // Multiplication
        int multiplicationResult = x * y;
        Console.WriteLine($"Multiplication Result: {multiplicationResult}");

        // Division
        int divisionResult = x / y;
        Console.WriteLine($"Division Result: {divisionResult}");

        // Modulus
        int modulusResult = x % y;
        Console.WriteLine($"Modulus Result: {modulusResult}");

        // Increment
        x++;
        Console.WriteLine($"Incremented x: {x}");

        // Decrement
        y--;
        Console.WriteLine($"Decremented y: {y}");
    }
}

Comparison Operators

Perform the comparison between two same type of values(variables).

This is important in any programming language to find answer and make the decision.

The comparison operator return boolean value either True or False. We will learn more in details about Boolean.

A list of all comparison operators:

Operator Name Example
== Equal to X == y
!= Not equal X != y
> Greater than X > y
< Less than X < y
>= Greater than or equal to X >= y
<= Less than or equal to X <= y
using System;

class Program
{
    static void Main()
    {
        // Declare and initialize variables
        int x = 10;
        int y = 5;

        // Equal to (==)
        bool isEqual = x == y;
        Console.WriteLine($"Equal to: {isEqual}");

        // Not equal (!=)
        bool isNotEqual = x != y;
        Console.WriteLine($"Not equal: {isNotEqual}");

        // Greater than (>)
        bool isGreaterThan = x > y;
        Console.WriteLine($"Greater than: {isGreaterThan}");

        // Less than (<) bool isLessThan=x < y;
        Console.WriteLine($"Less than: {isLessThan}");

        // Greater than or equal to (>=)
        bool isGreaterOrEqual = x >= y;
        Console.WriteLine($"Greater than or equal to: {isGreaterOrEqual}");

        // Less than or equal to (<=)
        bool isLessOrEqual = x <= y;
        Console.WriteLine($"Less than or equal to: {isLessOrEqual}");
    }
}

Boolean logical operators

3.perform logical operations with bool operands

Same as comparison operators, you can also get True or False as output using logical operators.

A list of all boolean logical operators:

Operator Name Description Example
&& Logical AND Return true if both statements are true A < 10 && B > 10
|| Logical OR Return true if any of the statements is true A < 10 || B > 10
! Logical Not Return reverse result. E.g. return true if the result is false and vice versa. !(a < 5 && b > 10)
using System;

class Program
{
    static void Main()
    {
        // Declare and initialize variables
        int A = 5;
        int B = 15;

        // Logical AND (&&)
        bool andResult = (A < 10) && (B > 10);
        Console.WriteLine($"Logical AND Result: {andResult}");

        // Logical OR (||)
        bool orResult = (A < 10) || (B > 10);
        Console.WriteLine($"Logical OR Result: {orResult}");

        // Logical NOT (!)
        bool notResult = !(A < 5 && B > 10);
        Console.WriteLine($"Logical NOT Result: {notResult}");
    }
}

Assignement Operator

Assignement operators are used to perform the value assignment to the variables.

C# provides below built-in assignment operators.

Operator Example Same As
= x = 5 x = 5
+= x += 3 x = x + 3
-= x -= 3 x = x - 3
*= x *= 3 x = x * 3
/= x /= 3 x = x / 3
%= x %= 3 x = x % 3
&= x &= 3 x = x & 3
|= x |= 3 x = x | 3
^= x ^= 3 x = x ^ 3
>= x >>= 3 x = x >> 3
<<= x <<= 3 x = x << 3
using System;

class Program
{
    static void Main()
    {
        // Declare and initialize variable
        int x = 5;

        // Assignment (=)
        int y = x;
        Console.WriteLine($"Assignment Result (y): {y}");

        // Addition Assignment (+=)
        x += 3;
        Console.WriteLine($"Addition Assignment Result (x): {x}");

        // Subtraction Assignment (-=)
        x -= 2;
        Console.WriteLine($"Subtraction Assignment Result (x): {x}");

        // Multiplication Assignment (*=)
        x *= 4;
        Console.WriteLine($"Multiplication Assignment Result (x): {x}");

        // Division Assignment (/=)
        x /= 2;
        Console.WriteLine($"Division Assignment Result (x): {x}");

        // Modulus Assignment (%=)
        x %= 3;
        Console.WriteLine($"Modulus Assignment Result (x): {x}");

        // Bitwise AND Assignment (&=)
        x &= 1;
        Console.WriteLine($"Bitwise AND Assignment Result (x): {x}");

        // Bitwise OR Assignment (|=)
        x |= 6;
        Console.WriteLine($"Bitwise OR Assignment Result (x): {x}");

        // Bitwise XOR Assignment (^=)
        x ^= 3;
        Console.WriteLine($"Bitwise XOR Assignment Result (x): {x}");

        // Right Shift Assignment (>>=)
        x >>= 1;
        Console.WriteLine($"Right Shift Assignment Result (x): {x}");

        // Left Shift Assignment (<<=)
        x <<= 2;
        Console.WriteLine($"Left Shift Assignment Result (x): {x}");
    }
}