Tuesday, January 2, 2024

Demystifying C# Operators: A Comprehensive Guide for Beginners

 

Demystifying C# Operators: A Comprehensive Guide for Beginners

Programming languages, including C#, rely on operators to perform various operations on data. Understanding these operators is fundamental to writing effective and efficient code. In this guide, we'll break down the key C# operators with simple examples to make them accessible for beginners.

Here's a list of various operators in C# along with brief descriptions. Following the list, I'll explain each operator with a code example.

List of C# Operators:

  1. 1.Arithmetic Operators:

    • + (Addition)
    • - (Subtraction)
    • * (Multiplication)
    • / (Division)
    • % (Modulus)
  2. 2.Comparison Operators:

    • == (Equal to)
    • != (Not equal to)
    • > (Greater than)
    • < (Less than)
    • >= (Greater than or equal to)
    • <= (Less than or equal to)
  3. 3.Logical Operators:

    • && (Logical AND)
    • || (Logical OR)
    • ! (Logical NOT)
  4. 4.Assignment Operators:

    • = (Assignment)
    • += (Add and assign)
    • -= (Subtract and assign)
    • *= (Multiply and assign)
    • /= (Divide and assign)
    • %= (Modulus and assign)
  5. 5.Increment and Decrement Operators:

    • ++ (Increment)
    • -- (Decrement)
  6. 6.Bitwise Operators:

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

Explanation with Code Examples:


using System; public class ArithmeticOperatorsExample { public static void Run() { int a = 10, b = 3; int sum = a + b; int difference = a - b; int product = a * b; int quotient = a / b; int remainder = a % b; Console.WriteLine($"Arithmetic Operators: Sum: {sum}, Difference: {difference}, Product: {product}, Quotient: {quotient}, Remainder: {remainder}"); } }

public  class ComparisonOperatorsExample { public static void Run() { int x = 5, y = 10; bool isEqual = x == y; bool isNotEqual = x != y; bool isGreater = x > y; bool isLess = x < y; bool isGreaterOrEqual = x >= y; bool isLessOrEqual = x <= y; Console.WriteLine($"Comparison Operators: Equal: {isEqual}, Not Equal: {isNotEqual}, Greater: {isGreater}, Less: {isLess}, Greater or Equal: {isGreaterOrEqual}, Less or Equal: {isLessOrEqual}"); } }

public class LogicalOperatorsExample { public static void Run() { bool isTrue = true, isFalse = false; bool andResult = isTrue && isFalse; bool orResult = isTrue || isFalse; bool notResult = !isTrue; Console.WriteLine($"Logical Operators: AND: {andResult}, OR: {orResult}, NOT: {notResult}"); } }

public class AssignmentOperatorsExample { public static void Run() { int num = 5; num += 3; num -= 2; num *= 4; num /= 2; num %= 3; Console.WriteLine($"Assignment Operators: Final Value: {num}"); } }

public class IncrementDecrementOperatorsExample { public static void Run() { int counter = 5; counter++; ++counter; counter--; --counter; Console.WriteLine($"Increment/Decrement Operators: Counter: {counter}"); } }

public class BitwiseOperatorsExample { public static void Run() { int a = 5, b = 3; int bitwiseAnd = a & b; int bitwiseOr = a | b; int bitwiseXor = a ^ b; int bitwiseNot = ~a; int leftShift = a << 1; int rightShift = a >> 1; Console.WriteLine($"Bitwise Operators: AND: {bitwiseAnd}, OR: {bitwiseOr}, XOR: {bitwiseXor}, NOT: {bitwiseNot}, Left Shift: {leftShift}, Right Shift: {rightShift}"); } }

public class Program { static void Main() { ArithmeticOperatorsExample.Run(); ComparisonOperatorsExample.Run(); LogicalOperatorsExample.Run(); AssignmentOperatorsExample.Run(); IncrementDecrementOperatorsExample.Run(); BitwiseOperatorsExample.Run(); } }


Labels:

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home