techmore.in

C# - Operators

In C#, operators are special symbols used to perform operations on variables and values. They are categorized into several types based on their functionality. Here’s a comprehensive guide to the different types of operators in C#:

1. Arithmetic Operators

Arithmetic operators are used to perform basic mathematical operations.

  • Addition (+): Adds two operands.

    csharp
    int sum = 5 + 3; // 8
  • Subtraction (-): Subtracts the second operand from the first.

    csharp
    int difference = 5 - 3; // 2
  • Multiplication (*): Multiplies two operands.

    csharp
    int product = 5 * 3; // 15
  • Division (/): Divides the first operand by the second.

    csharp
    int quotient = 15 / 3; // 5
  • Modulus (%): Returns the remainder of division.

    csharp
    int remainder = 5 % 3; // 2

2. Relational Operators

Relational operators are used to compare two values and determine their relationship.

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

    csharp
    bool isEqual = (5 == 3); // false
  • Not equal to (!=): Checks if two operands are not equal.

    csharp
    bool isNotEqual = (5 != 3); // true
  • Greater than (>): Checks if the first operand is greater than the second.

    csharp
    bool isGreater = (5 > 3); // true
  • Less than (<): Checks if the first operand is less than the second.

    csharp
    bool isLess = (5 < 3); // false
  • Greater than or equal to (>=): Checks if the first operand is greater than or equal to the second.

    csharp
    bool isGreaterOrEqual = (5 >= 3); // true
  • Less than or equal to (<=): Checks if the first operand is less than or equal to the second.

    csharp
    bool isLessOrEqual = (5 <= 3); // false

3. Logical Operators

Logical operators are used to perform logical operations on Boolean values.

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

    csharp
    bool result = (true && false); // false
  • Logical OR (||): Returns true if at least one operand is true.

    csharp
    bool result = (true || false); // true
  • Logical NOT (!): Returns the inverse of the Boolean value.

    csharp
    bool result = !true; // false

4. Bitwise Operators

Bitwise operators perform operations on the binary representations of integers.

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

    csharp
    int result = 5 & 3; // 1 (binary 0101 & 0011 = 0001)
  • Bitwise OR (|): Performs a bitwise OR operation.

    csharp
    int result = 5 | 3; // 7 (binary 0101 | 0011 = 0111)
  • Bitwise XOR (^): Performs a bitwise XOR operation.

    csharp
    int result = 5 ^ 3; // 6 (binary 0101 ^ 0011 = 0110)
  • Bitwise NOT (~): Inverts all the bits of the operand.

    csharp
    int result = ~5; // -6 (binary ~0101 = 1010, two's complement)
  • Left Shift (<<): Shifts bits to the left, adding zeros on the right.

    csharp
    int result = 5 << 1; // 10 (binary 0101 << 1 = 1010)
  • Right Shift (>>): Shifts bits to the right, discarding the rightmost bits.

    csharp
    int result = 5 >> 1; // 2 (binary 0101 >> 1 = 0010)

5. Assignment Operators

Assignment operators are used to assign values to variables.

  • Assignment (=): Assigns the value on the right to the variable on the left.

    csharp
    int x = 5;
  • Add and assign (+=): Adds the right operand to the left operand and assigns the result to the left operand.

    csharp
    x += 3; // x = x + 3
  • Subtract and assign (-=): Subtracts the right operand from the left operand and assigns the result to the left operand.

    csharp
    x -= 2; // x = x - 2
  • Multiply and assign (*=): Multiplies the left operand by the right operand and assigns the result to the left operand.

    csharp
    x *= 4; // x = x * 4
  • Divide and assign (/=): Divides the left operand by the right operand and assigns the result to the left operand.

    csharp
    x /= 2; // x = x / 2
  • Modulus and assign (%=): Takes the modulus of the left operand by the right operand and assigns the result to the left operand.

    csharp
    x %= 3; // x = x % 3

6. Increment and Decrement Operators

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

  • Increment (++): Increases the value of the operand by one. Can be used as a prefix or postfix.

    csharp
    int a = 5; a++; // a = 6 (postfix) ++a; // a = 7 (prefix)
  • Decrement (--): Decreases the value of the operand by one. Can be used as a prefix or postfix.

    csharp
    int b = 5; b--; // b = 4 (postfix) --b; // b = 3 (prefix)

7. Conditional (Ternary) Operator

The conditional operator (? :) is a shorthand for the if-else statement.

Syntax:

csharp
condition ? value_if_true : value_if_false;

Example:

csharp
int age = 20; string result = (age >= 18) ? "Adult" : "Minor"; // "Adult"

8. Null-Coalescing Operators

Null-coalescing operators provide a way to handle null values.

  • Null-coalescing (??): Returns the left operand if it is not null; otherwise, it returns the right operand.

    csharp
    string name = null; string displayName = name ?? "Default Name"; // "Default Name"
  • Null-coalescing assignment (??=): Assigns the right operand to the left operand only if the left operand is null.

    csharp
    string name = null; name ??= "Default Name"; // name = "Default Name"

9. Member Access Operators

  • Dot Operator (.): Accesses members (properties, methods) of an object or class.

    csharp
    Person person = new Person(); person.Name = "Alice"; // Accessing property person.Introduce(); // Calling method
  • Indexing Operator ([]): Accesses elements of an array or collection.

    csharp
    int[] numbers = { 1, 2, 3 }; int firstNumber = numbers[0]; // 1

Summary

Operators in C# are powerful tools for performing various operations on data. They include arithmetic, relational, logical, bitwise, assignment, increment/decrement, conditional, and null-coalescing operators. Understanding these operators and their proper usage is essential for writing effective and efficient C# code.