What is aggregation in C#?

Aggregation in C# represents a has-a relationship between objects where one class contains or uses another class. In aggregation, the contained object can exist independently of the container object, making it a weak association.

For example, an Employee has an Address, and a Department has multiple Employees. The key characteristic of aggregation is that when the container object is destroyed, the contained objects continue to exist independently.

Syntax

Following is the syntax for implementing aggregation in C# −

public class ContainerClass {
   private ContainedClass containedObject;
   
   public ContainerClass(ContainedClass obj) {
      this.containedObject = obj;
   }
}

Key Characteristics of Aggregation

  • Has-a relationship − One object contains a reference to another object.

  • Weak association − The contained object can exist without the container.

  • Independent lifecycle − Destroying the container doesn't destroy the contained object.

  • Shared ownership − Multiple containers can reference the same contained object.

Aggregation Relationship Employee - name: string - address: Address + Employee(Address) Address - street: string - city: string + GetFullAddress() has-a Address can exist without Employee Multiple Employees can share same Address

Using Aggregation with Employee and Address

Example

using System;

public class Address {
   public string Street { get; set; }
   public string City { get; set; }
   public string State { get; set; }

   public Address(string street, string city, string state) {
      Street = street;
      City = city;
      State = state;
   }

   public string GetFullAddress() {
      return Street + ", " + City + ", " + State;
   }
}

public class Employee {
   public string Name { get; set; }
   private Address address;

   public Employee(string name, Address addr) {
      Name = name;
      this.address = addr;
   }

   public void DisplayInfo() {
      Console.WriteLine("Employee: " + Name);
      Console.WriteLine("Address: " + address.GetFullAddress());
   }
}

public class Program {
   public static void Main() {
      Address addr1 = new Address("123 Main St", "New York", "NY");
      Address addr2 = new Address("456 Oak Ave", "Chicago", "IL");

      Employee emp1 = new Employee("John Doe", addr1);
      Employee emp2 = new Employee("Jane Smith", addr1); // Same address
      Employee emp3 = new Employee("Bob Johnson", addr2);

      emp1.DisplayInfo();
      Console.WriteLine();
      emp2.DisplayInfo();
      Console.WriteLine();
      emp3.DisplayInfo();
   }
}

The output of the above code is −

Employee: John Doe
Address: 123 Main St, New York, NY

Employee: Jane Smith
Address: 123 Main St, New York, NY

Employee: Bob Johnson
Address: 456 Oak Ave, Chicago, IL

Department and Employee Aggregation

Example

using System;
using System.Collections.Generic;

public class Employee {
   public string Name { get; set; }
   public int ID { get; set; }

   public Employee(int id, string name) {
      ID = id;
      Name = name;
   }

   public override string ToString() {
      return "ID: " + ID + ", Name: " + Name;
   }
}

public class Department {
   public string DeptName { get; set; }
   private List<Employee> employees;

   public Department(string deptName) {
      DeptName = deptName;
      employees = new List<Employee>();
   }

   public void AddEmployee(Employee emp) {
      employees.Add(emp);
   }

   public void DisplayEmployees() {
      Console.WriteLine("Department: " + DeptName);
      foreach (Employee emp in employees) {
         Console.WriteLine("  " + emp.ToString());
      }
   }
}

public class Program {
   public static void Main() {
      Employee emp1 = new Employee(101, "Alice Brown");
      Employee emp2 = new Employee(102, "Charlie Davis");

      Department dept = new Department("IT");
      dept.AddEmployee(emp1);
      dept.AddEmployee(emp2);

      dept.DisplayEmployees();
   }
}

The output of the above code is −

Department: IT
  ID: 101, Name: Alice Brown
  ID: 102, Name: Charlie Davis

Aggregation vs Composition

Aggregation Composition
Weak "has-a" relationship Strong "part-of" relationship
Child can exist without parent Child cannot exist without parent
Shared ownership possible Exclusive ownership
Example: Employee has Address Example: Car has Engine

Conclusion

Aggregation in C# represents a weak association where objects maintain independent lifecycles. It enables code reusability and flexible object relationships, making it ideal for scenarios where contained objects need to exist beyond their container's lifetime.

Updated on: 2026-03-17T07:04:35+05:30

513 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements