C# Aggregation

HTML
CSS
Bootstrap
JavaScript
C#
SQL
Salesforce Admin
Exercise
Study Material

Aggregation in C#

In C#, aggregation is a process in which one class defines another class as any entity reference. It is another way to reuse the class. It is a form of association that represents HAS-A relationship.

C# Aggregation Example

Let’s explore an example of aggregation in C# where the Car class contains a reference to the Engine class as a data member. This demonstrates how the Car class can reuse the functionalities and properties provided by the Engine class.

using System;

// Class representing an engine

public class Engine

{

    public string Model { get; set; }

    public int Horsepower { get; set; }

    // Method to start the engine

    public void Start()

    {

        Console.WriteLine(“Engine started.”);

    }

}

// Class representing a car

public class Car

{

    public string Make { get; set; }

    public string Model { get; set; }

    public Engine CarEngine { get; set; } // Aggregation: Car “has” an Engine

    // Constructor to initialize Car properties and Engine

    public Car(string make, string model, Engine engine)

    {

        Make = make;

        Model = model;

        CarEngine = engine;

    }

    // Method to start the car by delegating the start operation to the engine

    public void StartCar()

    {

        Console.WriteLine($”Starting {Make} {Model} {CarEngine.Model} {CarEngine.Horsepower}”);

        CarEngine.Start(); // Delegating start operation to the Engine object

    }

}

class Program

{

    static void Main()

    {

        // Create an instance of Engine

        Engine engine = new Engine { Model = “V8”, Horsepower = 400 };

        // Create an instance of Car and pass the Engine instance

        Car myCar = new Car(“Toyota”, “Camry”, engine);

       // Start the car

        myCar.StartCar(); // This will start the car’s engine

        // Keep the console window open

        Console.ReadLine();

    }

}

OUTPUT 

Practices Tasks

1. Hospital management system

For example: Design a hospital management system using classes: Doctor, Patient, and Hospital. The Doctor class should have properties for Name and Specialization, initialized via a constructor. The Patient class should include properties for Name and PatientId, also initialized via a constructor. The Hospital class should contain properties for Name and a list of Doctor objects (Doctors), initialized with a constructor. Additionally, implement methods in the Hospital class to AddDoctor(Doctor doctor) and TreatPatient(Doctor doctor, Patient patient), allowing doctors to be added to the hospital and patients to be treated by specific doctors. This system will model the relationship between hospitals, doctors, and patients, enabling the simulation of treatment interactions within the hospital environment.

OUTPUT 

2.Vehicle Rental System

For example: Design a vehicle rental system  using classes RentalCompany, Vehicle, and Customer, where a RentalCompany owns and rents out Vehicles to Customers. The Vehicle class should have properties for Model, Type, and DailyRate, initialized via a constructor. The RentalCompany class should include properties for Name and a list of Vehicles, with methods to AddVehicle(Vehicle vehicle) to its fleet and RentVehicle(Customer customer, Vehicle vehicle, int days) to rent a vehicle to a customer for a specified number of days. Create instances of RentalCompany, Vehicle, and Customer in the Main method to demonstrate the system’s functionality, simulating vehicle rentals and displaying rental details.

OUTPUT