Operators in C#.

Operators are a fundamental aspect of C# programming. They provide the means to manipulate data, make decisions, and control the flow of your programs. Operators are symbols or keywords that represent specific operations to be performed on operands. These operators are crucial for performing tasks like mathematical calculations, logical evaluations, and type conversions.


C# provides a rich set of operators that can be categorized into various groups, each serving a specific purpose. 

  • Arithmetic Operators
  • Comparison Operators
  • Logical Operators
  • Assignment Operators
  • Bitwise Operators
  • Conditional Operator
Operators are also categorized based on number of operands used in the operation. 
  • Unary Operator: The operator needs one operand to perform the operation. 
  • Binary Operator: The operator needs two operands to perform the operation.
  • Ternary Operator: The operator needs three operands to perform the operation.

Arithmetic Operators.

Arithmetic operators in C# are used to perform basic mathematical operations on numeric operands, such as addition, subtraction, multiplication, division, and modulus. These operators allow you to manipulate numeric data in your C# programs.

  • Addition (+): The addition operator is used to add two numeric values.
  • Subtraction (-): The subtraction operator is used to subtract the right operand from the left operand.
  • Multiplication (*): The multiplication operator is used to multiply two numeric values.
  • Division (/): The division operator is used to divide the left operand by the right operand.
  • Modulus (%): The modulus operator calculates the remainder when the left operand is divided by the right operand.
  • Increment (++) and Decrement (--): Increment and decrement operators are used to increase or decrease the value of a variable by 1. These are also known as urinary operators as they require only one operand to perform the operation.
Example Code:

// C# Code to show the working of Arithmetic Operators
using System;

public class AlgoLesson
{
    public static void Main(string[] args)
    {
        // Addition
        int sum = 5 + 3;
        Console.WriteLine ("Addition Operator: " + sum);
        
        // Subtraction
        int difference = 10 - 3;
        Console.WriteLine ("Subtraction Operator: " + difference);
        
        // Multiplication
        int product = 4 * 6;
        Console.WriteLine ("Production Operator: " + product);
        
        // Division
        int quotient = 15 / 3;
        Console.WriteLine ("Division Operator: " + quotient);
        
        int x = 5;
        // Increment Operator
        x++; // x becomes 6
        Console.WriteLine ("Increment Operator: " + x);
        
        int y = 10;
        // Decrement Operator
        y--; // y becomes 9
        Console.WriteLine ("Decrement Operator: " + y);
    }
}
Output:
Addition Operator: 8
Subtraction Operator: 7
Production Operator: 24
Division Operator: 5
Increment Operator: 6
Decrement Operator: 9

Comparison Operators.

Comparison operators in C# are used to compare two values or expressions and determine the relationship between them. These operators return a Boolean result, true or false, indicating whether the comparison is true or false. Here's a list of comparison operators in C#:
  • Equal (==): The equal operator checks if two values are equal.
  • Not Equal (!=): The not equal operator checks if two values are not equal.
  • Greater Than (>): The greater than operator checks if the left operand is greater than the right operand.
  • Less Than (<): The less than operator checks if the left operand is less than the right operand.
  • Greater Than or Equal To (>=): The greater than or equal to operator checks if the left operand is greater than or equal to the right operand.
  • Less Than or Equal To (<=): The less than or equal to operator checks if the left operand is less than or equal to the right operand.
Example Code:
// C# Code to show the working of Comparison Operators
using System;

public class AlgoLesson
{
    public static void Main(string[] args)
    {
        int a = 5;
        int b = 7;
        bool result;
        
        // Equal Operator
        result = (a == b); 
        Console.WriteLine("Equal to Operator: " + result);
        
        // Not Equal Operator
        result = (a != b);
        Console.WriteLine("Not Equal to Operator: " + result);
        
        // Greater Than Operator
        result = (a > b);
        Console.WriteLine("Greater Than Operator: " + result);
        
        // Less Than Operator
        result = (a < b);
        Console.WriteLine("Less Than Operator: " + result);
        
        // Greater Than or Equal To Operator
        result = (a >= b);
        Console.WriteLine("Greater Than or Equal To Operator: " + result);
        
        // Less Than or Equal To
        result = (a <= b);
        Console.WriteLine("Less Than or Equal To: " + result);
    }
}
Output:
Equal to Operator: False
Not Equal to Operator: True
Greater Than Operator: False
Less Than Operator: True
Greater Than or Equal To Operator: False
Less Than or Equal To: True

Logic Operators.

Logical operators in C# are used to perform logical operations on Boolean values (true or false). They allow you to combine multiple Boolean expressions to make more complex decisions. Here's a list of logical operators in C# along with examples:
  • Logical AND (&&): The logical AND operator returns true if both operands are true, otherwise, it returns false.
  • Logical OR (||): The logical OR operator returns true if at least one of the operands is true, otherwise, it returns false.
  • Logical NOT (!): The logical NOT operator returns the opposite of the operand. If the operand is true, it returns false, and vice versa.
Example Code:
// C# code to show to working of Logical Operators
using System;

public class AlgoLesson
{
    public static void Main(string[] args)
    {
        bool a = true, b = false, result;
        
        // AND Operator
        result = (a && b);
        Console.WriteLine ("AND Operator: " + result);
        
        // OR Operator
        result = (a || b);
        Console.WriteLine ("OR Operator: " + result);
        
        // NOT Operator
        result = (!a);
        Console.WriteLine ("NOT Operator: " + result);
    }
}
Output:
AND Operator: False
OR Operator: True
NOT Operator: False

Logical operators are often used in conditional statements to create more complex conditions.

Example Code:
// C# code to show to working of Logical Operators with Conditional Operators
using System;

public class AlgoLesson
{
    public static void Main(string[] args)
    {
        int age = 25;
        bool isStudent = false;

        if (age >= 18 && !isStudent)
        {
           Console.WriteLine("You are eligible for a driver's license.");
        }
    }
}
Output:
You are eligible for a driver's license.

Assignment Operators.

Assignment operators in C# are used to assign values to variables. They allow you to update the value of a variable based on some operation. Here's a list of assignment operators in C# along with examples:
  • Assignment (=): The assignment operator (=) is used to assign the value on the right-hand side to the variable on the left-hand side.
  • Addition Assignment (+=): The addition assignment operator (+=) is used to add the value on the right-hand side to the variable on the left-hand side and update the variable with the result.
  • Subtraction Assignment (-=): The subtraction assignment operator (-=) is used to subtract the value on the right-hand side from the variable on the left-hand side and update the variable with the result.
  • Multiplication Assignment (*=): The multiplication assignment operator (*=) is used to multiply the variable on the left-hand side by the value on the right-hand side and update the variable with the result.
  • Division Assignment (/=): The division assignment operator (/=) is used to divide the variable on the left-hand side by the value on the right-hand side and update the variable with the result.
  • Modulus Assignment (%=): The modulus assignment operator (%=) is used to find the remainder when the variable on the left-hand side is divided by the value on the right-hand side, and then update the variable with the result.

Example Code:
// C# code to show the working of Assignment Operators
using System;

public class AlgoLesson
{
    public static void Main(string[] args)
    {
        // Assigns the value 10 to the variable x
        int x = 10; 
        Console.WriteLine("Assignment Operator: " + x);
        
        // Equivalent to y = y + 3, updates y to 8
        int y = 5;
        y += 3; 
        Console.WriteLine("Add Assignment Operator: " + y);
        
        // Equivalent to z = z - 4, updates z to 6
        int z = 10;
        z -= 4; 
        Console.WriteLine("Subtraction Assignment Operator: " + z);
        
        // Equivalent to a = a * 2, updates a to 12
        int a = 6;
        a *= 2; 
        Console.WriteLine("Multiplication Assignment Operator: " + a);
        
        // Equivalent to b = b / 4, updates b to 5
        int b = 20;
        b /= 4; 
        Console.WriteLine("Division Assignment Operator: " + b);
        
        // Equivalent to c = c % 7, updates c to 1
        int c = 15;
        c %= 7; 
        Console.WriteLine("Modulus Assignment Operator: " + c);
    }
}
Output:
Assignment Operator: 10
Add Assignment Operator: 8
Subtraction Assignment Operator: 6
Multiplication Assignment Operator: 12
Division Assignment Operator: 5
Modulus Assignment Operator: 1

Assignment operators are commonly used to perform calculations and update variables in a concise manner. They are particularly useful in loops and other situations where variables need to be modified repeatedly.

Bitwise Operators.

Bitwise operators in C# allow you to perform operations on individual bits of integer types. Here's a list of bitwise operators in C# along with examples:
  • Bitwise AND (&): The bitwise AND operator (&) compares each bit of the first operand to the corresponding bit of the second operand. If both bits are 1, the result is 1; otherwise, it's 0.
  • Bitwise OR (|): The bitwise OR operator (|) compares each bit of the first operand to the corresponding bit of the second operand. If either bit is 1, the result is 1; otherwise, it's 0.
  • Bitwise XOR (^): The bitwise XOR operator (^) compares each bit of the first operand to the corresponding bit of the second operand. If the bits are different (one is 0 and the other is 1), the result is 1; otherwise, it's 0.
  • Bitwise NOT (~): The bitwise NOT operator (~) inverts each bit of the operand. It changes 0s to 1s and 1s to 0s.
  • Left Shift (<<): The left shift operator (<<) shifts the bits of the first operand to the left by the number of positions specified by the second operand.
  • Right Shift (>>): The right shift operator (>>) shifts the bits of the first operand to the right by the number of positions specified by the second operand. If the leftmost bit is 0, it fills with 0; if it's 1, it fills with 1.
Example Code:
// C# Code to explain working of Bitwise Operators

using System;

public class AlgoLesson
{
    public static void Main(string[] args)
    {
        int a = 5;    // Binary: 0101
        int b = 3;    // Binary: 0011
        int result;
        
        // Bitwise AND Operator
        result = a & b; 
        Console.WriteLine ("Bitwise AND Operator: " + result);
        
        // Bitwise OR Operator
        result = a | b;
        Console.WriteLine ("Bitwise OR Operator: " + result);
        
        // Bitwise XOR Operator
        result = a ^ b; 
        Console.WriteLine ("Bitwise XOR Operator: " + result);
        
        // Bitwise NOT Operator
        result = ~a;
        Console.WriteLine ("Bitwise NOT Operator: " + result);
        
        // Left Shift Operator
        int leftShifted = a << 2;
        Console.WriteLine ("Bitwise Left Shift Operator: " + leftShifted);
        
        // Right Shift Operator
        int rightShifted = a >> 2;
        Console.WriteLine ("Bitwise Right Shift Operator: " + rightShifted);
    }
}
Output:
Bitwise AND Operator: 1
Bitwise OR Operator: 7
Bitwise XOR Operator: 6
Bitwise NOT Operator: -6
Bitwise Left Shift Operator: 20
Bitwise Right Shift Operator: 1

Conditional Operators.

In C#, the conditional operator, also known as the ternary operator, is a concise way to express conditional statements. It's often used to make decisions in a single line of code.

Syntax:
condition ? expression1 : expression2

Working: The condition is evaluated first. If it's true, expression1 is executed; if it's false, expression2 is executed.

Example Code:
// C# Code to explain working of Ternary Operators

using System;

public class AlgoLesson
{
    public static void Main(string[] args)
    {
        int num = 10;
        // Conditional Operator
        string result = (num % 2 == 0) ? "Even" : "Odd";
        Console.WriteLine(result);
    }
}
Output:
Even


Null Coalescing Operator (??)

The null coalescing operator (??) is used to provide a default value for nullable types or reference types that might be null.

Example Code:
// C# Code to explain working of Null Coalescing Operator

using System;

public class AlgoLesson
{
    public static void Main(string[] args)
    {
        int? nullableValue = null;
        
        int result = nullableValue ?? 10; // result will be 10
        
        Console.WriteLine(result);
    }
}
Output:
10

This guide has covered a wide range of operators in C#, including arithmetic, comparison, logical, assignment, bitwise, and more. Each operator serves a specific purpose and can be a valuable tool in your programming toolkit.

⚡ Please share your valuable feedback and suggestion in the comment section below or you can send us an email on our offical email id ✉ algolesson@gmail.com. You can also support our work by buying a cup of coffee ☕ for us.

Similar Posts

No comments:

Post a Comment


CLOSE ADS
CLOSE ADS