UNIT 2
Here’s a more detailed and expanded version of your
answers, tailored for 5-mark questions.
1. Properties of a Class in C#
In C#, a class is a blueprint or template used to create
objects. Classes contain fields, methods, properties,
constructors, and events that define the behavior of an
object. The properties of a class refer to its attributes or
features that define its functionality. The key properties of a
class include:
     Encapsulation: The process of bundling the data (fields)
      and methods (functions) that operate on the data within
      one unit or class. This hides the internal state of an
      object and only exposes a controlled interface for
      interacting with it.
     Inheritance: In C#, classes can inherit properties and
      methods from other classes. The derived class inherits
      the behavior of the base class, making it easier to reuse
      and extend existing code.
     Polymorphism: It allows the same method to behave
      differently based on the object that invokes it. There are
      two types of polymorphism in C#: compile-time (method
      overloading) and runtime (method overriding).
       Abstraction: Abstraction means hiding the
        implementation details from the user and providing a
        simple interface. It helps in focusing on the essential
        features of an object, ignoring unnecessary details.
       Access Modifiers: Access modifiers in C# control the
        visibility of members (methods, properties, etc.) of a
        class. These modifiers are used to specify the
        accessibility level (public, private, protected, etc.) of
        each member.
       Constructors: A constructor is a special method used for
        initializing objects. It is called automatically when an
        object is created. Constructors can be overloaded with
        different parameters to initialize objects in various ways.
Example:
public class Car
{
    public string make;
    public string model;
    private int year;
    public Car(string make, string model, int year)
    {
        this.make = make;
        this.model = model;
        this.year = year;
    }
    public void DisplayInfo()
    {
    Console.WriteLine($"Car Make: {make}, Model: {model},
Year: {year}");
    }
}
2. Prefix and Postfix Notations
Prefix and postfix notations refer to how operators are placed
relative to their operands. These notations are used primarily
for increment and decrement operations in C#.
       Prefix notation: The operator is placed before the
        operand. The operand is first modified by the operator,
        and then the result is returned. For example, ++a
        increments the value of a and then returns the new
        value of a.
Example:
int a = 5;
int b = ++a; // Prefix notation
Console.WriteLine(b); // Output: 6 (a is incremented first,
then b gets the new value)
     Postfix notation: The operator is placed after the
      operand. The current value of the operand is first
      returned, and then the operand is modified. For
      example, a++ returns the current value of a and then
      increments a.
Example:
int a = 5;
int b = a++; // Postfix notation
Console.WriteLine(b); // Output: 5 (b gets the current value
of a, then a is incremented)
Key Difference:
     In prefix notation, the increment (or decrement) occurs
      before the value is used.
     In postfix notation, the value is used before the
      increment (or decrement) takes place.
3. Conditional Statements
Conditional statements allow the program to execute
different sets of instructions based on whether a condition is
true or false. They help in decision-making by controlling the
flow of the program. Common conditional statements in C#
are if, else, else if, and switch.
     if statement: Executes a block of code if a specified
      condition evaluates to true.
       else if statement: Provides additional conditions to test
        if the initial if condition is false.
       else statement: Executes a block of code if none of the
        previous conditions are true.
       switch statement: Provides an alternative to multiple if-
        else statements when checking a single variable for
        multiple possible values.
Example:
int age = 18;
if (age >= 18)
{
    Console.WriteLine("You are eligible to vote.");
}
else
{
    Console.WriteLine("You are not eligible to vote.");
}
       In the above example, if the condition age >= 18 is true,
        the program prints "You are eligible to vote". If the
        condition is false, it prints "You are not eligible to vote".
4. Operators and Their Types
Operators in C# are special symbols used to perform
operations on variables and values. They can be classified
into several categories:
     Arithmetic Operators: Used to perform basic
      mathematical operations like addition, subtraction,
      multiplication, and division.
        o   Example:
        o   int sum = 5 + 3; // Arithmetic addition operator
        o   int product = 5 * 3; // Arithmetic multiplication
            operator
     Relational Operators: Used to compare two values. They
      return true or false based on the result of the
      comparison.
        o   Example:
        o   bool isEqual = (5 == 5); // Equality comparison
            operator
        o   bool isGreaterThan = (10 > 5); // Greater than
            comparison operator
     Logical Operators: Used to perform logical operations,
      typically used in conditional statements.
        o   Example:
        o   if (a > 5 && b < 10) { /* do something */ } // Logical
            AND operator
     Unary Operators: Perform operations on a single
      operand, such as increment (++), decrement (--), or
      negation (!).
        o   Example:
        o   int i = 5;
        o   i++; // Postfix increment operator
     Assignment Operators: Used to assign values to
      variables. = is the most common assignment operator,
      while +=, -= allow shorthand operations.
        o   Example:
        o   int x = 5; // Simple assignment
        o   x += 3; // x = x + 3
     Bitwise Operators: Operate on the binary
      representation of integers. Common bitwise operators
      include &, |, ^, ~, <<, and >>.
        o   Example:
        o   int result = 5 & 3; // Bitwise AND
5. Access Modifiers and Their Types
Access modifiers are keywords used to define the accessibility
or visibility of classes, methods, and other members. They
control how and where members of a class can be accessed.
     Public: The public access modifier allows unrestricted
      access to the members of the class from anywhere. This
      is the most permissive access level.
      o   Example:
      o   public class MyClass
      o   {
      o       public int number;
      o   }
   Private: The private access modifier restricts access to
    the members of the class only within the class itself.
    Other classes cannot access these members.
      o   Example:
      o   public class MyClass
      o   {
      o       private int number;
      o   }
   Protected: The protected access modifier allows access
    to members of the class from the class itself and from
    derived classes.
      o   Example:
      o   public class ParentClass
      o   {
      o       protected int number;
      o   }
     Internal: The internal access modifier allows access to
      members from within the same assembly (project). It is
      not accessible from other assemblies.
        o   Example:
        o   internal class MyClass
        o   {
        o       internal int number;
        o   }
     Protected Internal: Combines the features of both
      protected and internal, making a member accessible
      within the same assembly and by derived classes.
        o   Example:
        o   protected internal int number;
     Private Protected: A combination of private and
      protected modifiers. It restricts access to the class itself
      and derived classes within the same assembly.
        o   Example:
        o   private protected int number;
These modifiers help ensure data encapsulation and security
by controlling access to class members.
6. Constructors
In C#, a constructor is a special method used to initialize
objects. It is called when an instance of the class is created.
Constructors do not return a value. They have the same name
as the class and can be overloaded to accept different
parameters for object initialization.
       Default Constructor: A constructor without parameters.
       Parameterized Constructor: A constructor that accepts
        parameters to initialize an object with specific values.
Example:
public class Car
{
    public string make;
    public string model;
    private int year;
    // Parameterized Constructor
    public Car(string make, string model, int year)
    {
        this.make = make;
        this.model = model;
        this.year = year;
    }
    public void DisplayInfo()
    {
    Console.WriteLine($"Make: {make}, Model: {model},
Year: {year}");
    }
}
Car myCar = new Car("Toyota", "Camry", 2020); //
Constructor called to initialize the object
myCar.DisplayInfo();
7. Method Overloading
Method overloading is a feature that allows multiple
methods in the same class to have the same name but with
different parameter types or numbers. This enables methods
to perform similar tasks with different types of inputs.
Example:
public class Calculator
{
    public int Add(int a, int b)
    {
        return a + b;
    }
    public double Add(double a, double b)
    {
        return a + b;
    }
    public string Add(string a, string b)
    {
        return a + b;
    }
}
Here, the Add method is overloaded with different parameter
types: integers, doubles, and strings.
8. Method Overloading Program Example
public class Calculator
{
    // Overloaded Add method with two integers
    public int Add(int a, int b)
    {
        return a + b;
    }
    // Overloaded Add method with two doubles
    public double Add(double a, double b)
    {
        return a + b;
    }
    // Overloaded Add method with two strings
    public string Add(string a, string b)
    {
        return a + b;
    }
}
class Program
{
    static void Main()
    {
        Calculator calc = new Calculator();
        Console.WriteLine(calc.Add(2, 3));    // int addition
    Console.WriteLine(calc.Add(2.5, 3.5));     // double
addition
    Console.WriteLine(calc.Add("Hello", "World")); // string
concatenation
    }
}
9. Loops in C#
Loops in C# are used to repeatedly execute a block of code
based on a condition. The most commonly used loops are for,
while, and do-while loops.
       For Loop: A for loop is used when the number of
        iterations is known beforehand.
       for (int i = 0; i < 5; i++)
       {
           Console.WriteLine(i); // Output: 0 1 2 3 4
       }
       While Loop: A while loop repeats a block of code as long
        as the condition is true. It checks the condition before
        the first iteration.
       int i = 0;
       while (i < 5)
       {
           Console.WriteLine(i); // Output: 0 1 2 3 4
           i++;
       }
       Do-While Loop: A do-while loop executes the block of
        code at least once and then checks the condition.
   int i = 0;
   do
   {
       Console.WriteLine(i); //