C# Operators (original) (raw)

Last Updated : 13 Jan, 2025

In C#, Operators are special types of symbols which perform operations on variables or values. It is a fundamental part of language which plays an important role in performing different mathematical operations. It takes one or more operands and performs operations to produce a result.

Types of Operators

C# has some set of operators that can be classified into various categories based on their functionality. Categorized into the following types:

  1. Arithmetic Operators
  2. Relational Operators
  3. Logical Operators
  4. Assignment Operators
  5. Increment and Decrement Operators
  6. Bitwise Operators
  7. Ternary Operator
  8. Null Coalescing Operator
  9. Conditional Logical Operators

1. Arithmetic Operators

Arithmetic operators are used to perform basic mathematical operations on numeric values.

**Example:

C# `

// Arithmetic operators using System;

class Geeks { static void Main(string[] args) { int x = 8, y = 4;

    // Using different arithmetic operators
    Console.WriteLine("Addition: " + (x + y));
    Console.WriteLine("Subtraction: " + (x - y));
    Console.WriteLine("Multiplication: " + (x * y));
    Console.WriteLine("Division: " + (x / y));
    Console.WriteLine("Modulo: " + (x % y));
}

}

`

Output

Addition: 15 Subtraction: 5 Multiplication: 50 Division: 2 Modulo: 0

2. Relational Operators

Relational operators are used to compare values. And we get the answer in either true or false ( boolean). Let's learn about different relation operators.

**Example:

C# `

// Relational operators using System;

class Geeks { static void Main(string[] args) { int x = 10, y = 20;

    // Compare using different
      // relational operators
    Console.WriteLine(x == y);
    Console.WriteLine(x != y);
    Console.WriteLine(x > y);
    Console.WriteLine(x < y);
    Console.WriteLine(x >= y);
    Console.WriteLine(x <= y);
}

}

`

Output

False True False True False True

3. Logical Operators

Used when multiple conditions and there we can combine these to compare complex conditions.

**Example:

C# `

// Conditional Operators using System;

class Geeks { static void Main(string[] args) { bool a = true, b = false;

    // conditional operators
    if (a && b)
        Console.WriteLine("a and b are true");

    if (a || b)
        Console.WriteLine("Either a or b is true");

    if (!a)
        Console.WriteLine("a is not true");
    if (!b)
        Console.WriteLine("b is not true");
}

}

`

Output

Either a or b is true b is not true

4. Assignment Operators

Assignment operators are used to assign values to variables. The assignment operator is combined with others to create shorthand compound statements. Common compound operators include:

**Example:

C# `

// Assignment Operators using System;

class Geeks { static void Main(string[] args) { int a = 10;

    // Using different assignment operators
    a += 5;
    Console.WriteLine("Add Assignment: " + a);

    a -= 3;
    Console.WriteLine("Subtract Assignment: " + a);

    a *= 2;
    Console.WriteLine("Multiply Assignment: " + a);

    a /= 4;
    Console.WriteLine("Division Assignment: " + a);

    a %= 5;
    Console.WriteLine("Modulo Assignment: " + a);
}

}

`

Output

Add Assignment: 15 Subtract Assignment: 12 Multiply Assignment: 24 Division Assignment: 6 Modulo Assignment: 1

5. Increment/Decrement Operators

Increment and decrement operators are used to increase or decrease the value of a variable by 1.

**Example:

C# `

// Increment and decrement // operators using System;

public class Geeks { static public void Main() { int a = 5;

      // pre-increment
    Console.WriteLine("++a returns: " + ++a);
  
      // post-increment
    Console.WriteLine("a++ returns: " + a++);

      Console.WriteLine("Final value of a: " + a);
  
      Console.WriteLine();
  
    // pre-decrement
      Console.WriteLine("--a returns: " + --a);
  
      // post-decrement
      Console.WriteLine("a-- returns: " + a--);
      
      Console.WriteLine("Final value of a: " + a);
}

}

`

Output

++a returns: 6 a++ returns: 6 Final value of a: 7

--a returns: 6 a-- returns: 6 Final value of a: 5

6. Bitwise Operators

Bitwise operators are used to perform bit-level operations on integer values. It takes less time because it directly works on the bits.

**Example:

C# `

// Bitwise Operators using System;

class Geeks { static void Main(string[] args) { // Binary representation: 1010 int x = 10;

    // Binary representation: 0010
    int y = 2;

    // Bitwise AND 
    Console.WriteLine(x & y);

    // Bitwise OR 
    Console.WriteLine(x | y);

    // Bitwise XOR 
    Console.WriteLine(x ^ y);

    // Bitwise NOT 
    Console.WriteLine(~x);

    // Shifting bit by one on the left
    Console.WriteLine(x << 1);

    // Shifting bit by one on the right
    Console.WriteLine(x >> 1);
}

}

`

7. Ternary Operator

The ternary operator is a shorthand for an if-else statement. It evaluates a condition and returns one of two values depending on whether the condition is true or false.

Syntax

condition ? if true : if false

**Example:

C# `

// Ternary Operator using System;

public class Geeks { static public void Main() { int a = 10, b = 5; // similar to if else

    string result = (a > b) ? "a" : "b";
    Console.WriteLine(result + " is greater");
}

}

`

8. Null-Coalescing Operator

null-coalescing operator is used when we want to put a **default value if the value of the variable is null.

**Example:

C# `

// Null-Coalescing Operator using System;

public class Geeks { static public void Main() { string name = null;

    // Name have null value takes default
      // value instead of null
    string result = name ? ? "Default Name";
    Console.WriteLine(result);
}

}

`

9. Logical Operators

Logical operators are used to combine multiple boolean expressions. The common logical operators include:

**Example:

C# `

// Logical Operators using System;

class Geeks { static void Main(string[] args) { bool a = true, b = false;

    // return true when both condtions are true
    Console.WriteLine(a && b);

    // return true if any of one is true
    Console.WriteLine(a || b);

    // perform conjuction and negate the condition
    Console.WriteLine(!a);
}

}

`

Operator Associativity

The following table summarizes the associativity of various operators in C#:

Operator Type Operators Associativity
Postfix Operators ++, -- Left to Right
Unary Operators + , - , ! , ! Right to Left
Multiplicative Operators * , / , % Left to Right
Additive Operators + , - Left to Right
Shift Operators << , >> Left to Right
Relational Operators < , > , <= , >= Left to Right
Equality Operators ==, != Left to Right
Bitwise operator & , | , ^ Left to Right
Logical Operator && , |
Conditional Operator " ? " Right to Left
Assignment Operators =, +=, -=, *=, /=, %=, ... Right to Left

Explanation of Associativity

Operator Precedence

Operator precedence is the most important to evaluate any expression because it gives us an idea about how the different operation performs and which one has the higher precedence.

The below table shows the precedence of different operators in C# in ascending order.

Operator Description
(), [], . Parentheses, Array indexing, Member access
++, --, !, ~ Unary increment/decrement, logical NOT, bitwise NO.T
*, /, % Multiplication, Division, Modulus
+, - Addition, Subtraction
==, !=, >, < Relational operators

**Example:

C# `

// Operator Precedence using System;

class Geeks { static void Main() { int a = 2, b = 4, c = 8;

    // Multiplication precedence is higher 
    // so it performed first
    int ans = a + b * c;
  
    Console.WriteLine("Ans of a + b * c: " + ans);
   
    // Addition has higher precedence
    if( a + b > c)         
       Console.WriteLine(a + b);       

}

}

`

Output

Ans of a + b * c: 34

Best Practices