C# Tutorial

C# String

C# Array

C# Flow Control

C# Class and Object

C# Inheritance

C# Interface

C# Collection

C# Generic

C# File I/O

C# Delegate and Event

C# Exception

C# Process and Thread

C# ADO.NET Database Operations

C# Operators

In C#, operators are special symbols that perform specific operations on operands and return a value. They allow you to manipulate data and perform calculations. In this tutorial, we'll cover the following types of operators in C#:

  • Arithmetic operators
  • Relational operators
  • Logical operators
  • Bitwise operators
  • Assignment operators
  • Other operators

Let's begin!

  • Arithmetic operators

Arithmetic operators are used to perform basic mathematical operations:

  • +: Addition
  • -: Subtraction
  • *: Multiplication
  • /: Division
  • %: Modulus (remainder)

Example:

int a = 10;
int b = 3;

int sum = a + b; // 13
int difference = a - b; // 7
int product = a * b; // 30
int quotient = a / b; // 3
int remainder = a % b; // 1
  • Relational operators

Relational operators are used to compare values and return a boolean result:

  • ==: Equal to
  • !=: Not equal to
  • <: Less than
  • >: Greater than
  • <=: Less than or equal to
  • >=: Greater than or equal to

Example:

int a = 10;
int b = 3;

bool isEqual = a == b; // false
bool isNotEqual = a != b; // true
bool isLessThan = a < b; // false
bool isGreaterThan = a > b; // true
bool isLessThanOrEqual = a <= b; // false
bool isGreaterThanOrEqual = a >= b; // true
  • Logical operators

Logical operators are used to combine boolean expressions:

  • &&: Logical AND
  • ||: Logical OR
  • !: Logical NOT

Example:

bool a = true;
bool b = false;

bool andResult = a && b; // false
bool orResult = a || b; // true
bool notResult = !a; // false
  • Bitwise operators

Bitwise operators perform operations on the individual bits of integer operands:

  • &: Bitwise AND
  • |: Bitwise OR
  • ^: Bitwise XOR
  • ~: Bitwise NOT (complement)
  • <<: Left shift
  • >>: Right shift

Example:

int a = 5; // 0101 in binary
int b = 3; // 0011 in binary

int andResult = a & b; // 1 (0001 in binary)
int orResult = a | b; // 7 (0111 in binary)
int xorResult = a ^ b; // 6 (0110 in binary)
int notResult = ~a; // -6 (in two's complement representation)
int leftShiftResult = a << 1; // 10 (1010 in binary)
int rightShiftResult = a >> 1; // 2 (0010 in binary)
  • Assignment operators

Assignment operators are used to assign a value to a variable:

  • =: Assign
  • +=: Add and assign
  • -=: Subtract and assign
  • *=: Multiply and assign
  • /=: Divide and assign
  • %=: Modulus and assign
  • &=: Bitwise AND and assign
  • |=: Bitwise OR and assign
  • ^=: Bitwise XOR and assign
  1. Types of operators in C#

    Operators in C# are classified into various types, including arithmetic, relational, logical, bitwise, assignment, and more.

  2. Arithmetic operators in C#

    Arithmetic operators perform basic mathematical operations.

    int result = 10 + 5; // Addition
    
  3. Relational operators in C#

    Relational operators compare values and return a boolean result.

    bool isGreaterThan = 10 > 5; // Greater than
    
  4. Logical operators in C#

    Logical operators perform logical operations and return a boolean result.

    bool logicalResult = true && false; // Logical AND
    
  5. Bitwise operators in C#

    Bitwise operators perform operations at the bit level.

    int bitwiseResult = 5 & 3; // Bitwise AND
    
  6. Assignment operators in C#

    Assignment operators assign values to variables.

    int x = 10;
    
  7. Increment and decrement operators in C#

    Increment and decrement operators modify the value of a variable.

    int x = 5;
    x++; // Increment
    
  8. Ternary conditional operator in C#

    The ternary conditional operator is a concise way to express conditional statements.

    int result = (x > 0) ? 1 : -1;
    
  9. Null-coalescing operator in C#

    The null-coalescing operator provides a default value if an expression is null.

    int? nullableValue = null;
    int result = nullableValue ?? 0;
    
  10. Overloading operators in C#

    Overloading operators allows custom behavior for user-defined types.

    public static MyClass operator +(MyClass a, MyClass b)
    {
        // Custom addition logic
    }
    
  11. Custom operators in C#

    Custom operators can be defined for user-defined types to perform specific operations.

    public static MyClass operator +(MyClass a, MyClass b)
    {
        // Custom addition logic
    }
    
  12. Operator precedence in C#

    Operator precedence determines the order in which operators are evaluated in an expression.

    int result = 2 + 3 * 4; // Multiplication has higher precedence
    
  13. Operator overloading vs. method overloading in C#

    • Operator Overloading:

      public static MyClass operator +(MyClass a, MyClass b)
      {
          // Custom addition logic
      }
      
    • Method Overloading:

      public int Add(int a, int b)
      {
          // Standard addition logic
      }
      
      public string Add(string a, string b)
      {
          // Concatenation logic
      }