C# Operators

In C#, operators are special symbols which helps to perform operations on variables and values, means its tells the compiler to perform specific operation like mathematical or logical manipulations. They are classified into several categories, including arithmetic operators, relational operators, logical operators, assignment operators, and more.

C# has rich set of built-in operators and provides the following type of operators −


  • Arithmetic Operators
  • Relational Operators
  • Logical Operators
  • Assignment Operators
  • Unary Operators
  • Conditional Operator
  • Bitwise Operators
  • Null-Coalescing Operator
  • Null Conditional Operator

This tutorial explains the arithmetic, relational, logical, bitwise, assignment, and other operators one by one in C# with examples:

  1. 1. Arithmetic Operators

    These operators are used for basic mathematical operations.

    • Addition (+): Adds two operands.

    • Subtraction (-): Subtracts the right operand from the left operand.

    • Multiplication (*): Multiplies two operands.

    • Division (/): Divides the left operand by the right operand.

    • Modulus (%): Returns the remainder of a division.

    Example:
    
    int a = 10;
    int b = 5;
    
    Console.WriteLine("Addition: " + (a + b));           // 10 + 5 = 15
    Console.WriteLine("Subtraction: " + (a - b));        // 10 - 5 = 5
    Console.WriteLine("Multiplication: " + (a * b));     // 10 * 5 = 50
    Console.WriteLine("Division: " + (a / b));           // 10 / 5 = 2
    Console.WriteLine("Modulus: " + (a % b));            // 10 % 5 = 0
    
    

  2. 2. Relational Operators

    These operators compare two values and then return a boolean result.

    • Equal to (==): Returns true if two operands are equal.

    • Not equal to (!=): > Returns true if two operands are not equal.

    • Greater than (>): • Returns true if the left operand is greater than the right operand.

    • Less than (<): Returns true if the right operand is greater than the left operand.

    • Greater than or equal to (>=): Returns true if the left operand is greater than or equal to the right operand.

    • Less than or equal to (<=): Returns true if the left operand is less than or equal to the right operand.

    Example:
    
    int x = 10;
    int y = 20;
    
    Console.WriteLine(x == y);           // false
    Console.WriteLine(x != y);        // true
    Console.WriteLine(x > y);     // false
    Console.WriteLine(x < y);           // true
    Console.WriteLine(x >= y);            // false
    Console.WriteLine(x <= y);            // true
    
    

  3. 3. Logical Operators

    These operators are used to perform logical operations, often in conditional statements.

    • AND (&&): Returns true if both the operands are

    • OR (||): Returns true if at least one operand is true.

    • NOT (!): Returns true Reverses the logical state of its operand.

    Example:
    
    bool a = true;
    bool b = false;
    
    Console.WriteLine(a && b);           // false
    Console.WriteLine(a || b);        // true
    Console.WriteLine(!a);     // false
    Console.WriteLine(!b);           // true
    
    

  4. 4. Assignment Operators

    These operators are used to assign values to variables.

    • Simple Assignment (=): Assigns the value of the right operand to the left operand.

    • Add and Assign (+=): Adds the right operand to the left operand and assigns the result to the left operand.

    • Subtract and Assign (-=): Subtracts the right operand from the left operand and assigns the result to the left operand.

    • Multiply and Assign (*=): Multiplies the left operand with the right operand and assigns the result to the left operand.

    • Divide and Assign (/=): Divides the left operand by the right operand and assigns the result to the left operand.

    Example:
    
    int a = 10;
    
    a += 5; // a = a + 5 
    Console.WriteLine (a); // 15
    a -= 3; //  a = a – 3 
    Console.WriteLine (a); // 12
    a *= 2; // a = a * 2 
    Console.WriteLine (a); // 24
    a /= 4; // a = a / 4
    Console.WriteLine (a); // 6
    
    

  5. 5.Unary Assignment Operators

    These operators act on a single operand and perform operations such as incrementing, decrementing, and negating.

    • Increment (++): Increases the value of the operand by 1.

    • Decrement (--): Decreases the value of the operand by 1.

    • Unary Plus (+): Indicates a positive value.

    • Unary Minus (-): Negates the value of the operand.

    Example:
    
    int a = 5;
    Console.WriteLine (++a); // 6 (pre-increment)
    int b = 10;
    Console.WriteLine (b++); // 10 (post-increment, b becomes 11 after)
    int c = 15;
    Console.WriteLine (--c); // 14 (pre-decrement)
    int d = 20;
    Console.WriteLine (c--); // 20 (post-decrement, d becomes 19 after)
    int e = 25;
    Console.WriteLine (-e); // -25 (negation)
    
    

  6. 6. Conditional (Ternary) Operator

    This operator is used as a shorthand for if-else statements.

    • Ternary (? :): Evaluates a condition and returns one of two values depending on whether the condition is true or false.

    Example:
    
    int a = 10;
    int b = 20;
    int max = (a > b) ? a : b;
    Console.WriteLine (max); // 20
    
    

  7. 7. Bitwise Operators

    These operators work on bits and perform bit-by-bit operations.

    • AND (&): Performs a bitwise AND operation.

    • OR (|): Performs a bitwise OR operation.

    • XOR (^): Performs a bitwise XOR operation.

    • Complement (~): Inverts all the bits..

    • Shift Left (<<): Shifts the bits to the left.

    • Shift Right (>>): Shifts the bits to the left.

    Example:
    
    int a = 5; // 0101 in binary
    int b = 3; // 0011 in binary
    Console.WriteLine (a & b); // 1 (0001 in binary) 
    Console.WriteLine (a | b); // 7 (0111 in binary) 
    Console.WriteLine (a ^ b);  // 6 (0110 in binary) 
    Console.WriteLine (~a); // -6 (inverts all bits of 5) 
    Console.WriteLine (a << 1);  // 10 (shift bits left by 1) 
    Console.WriteLine (a >> 1);  // 2 (shift bits right by 1) 
    
    

  8. 8. Null-Coalescing Operator

    The null-coalescing operator (??) is used to define a default value when the operand is null.

    Example:
    
    string str = null;
    string result = str ?? "Default Value";
    Console.WriteLine (result); // Default Value
    
    

  9. 9. Null Conditional Operator

    The null-conditional operator (?.) allows safe access to members or elements without throwing an exception if the object is null.

    Example:
    
    Person person = null;
     int? length = person?.Name?.Length; // Avoids null reference exception
    Console.WriteLine(length); // null
    
    

Conclusion

These are some of the operators commonly used in C# with their explanations and examples. There are also some additional operators, such as type cast operators, is/as, and others that can be useful depending on your needs.

An operator manipulates an arithmetic or logical value, or operand, in a specific way to generate a specific result. These operators makes developers life easy because it helps a developer in handling simple arithmetic functions to facilitating the execution of algorithms, like security encryption.