Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
What are the difference between Composition and Aggregation in C#?
Composition and Aggregation are two important types of associations in C# that represent different levels of dependency between classes. Understanding these relationships helps design better object-oriented systems with proper coupling and lifecycle management.
Composition
Composition represents a strong "part-of" relationship where the child object cannot exist independently of the parent object. When the parent is destroyed, all child objects are also destroyed automatically.
Example
using System;
public class Engine {
private string type;
public Engine(string type) {
this.type = type;
Console.WriteLine("Engine created: " + type);
}
public void Start() {
Console.WriteLine(type + " engine started");
}
}
public class Car {
private Engine engine; // Composition
private string model;
public Car(string model) {
this.model = model;
this.engine = new Engine("V6"); // Car creates and owns Engine
Console.WriteLine("Car created: " + model);
}
public void StartCar() {
Console.WriteLine("Starting " + model);
engine.Start();
}
}
public class Program {
public static void Main() {
Car myCar = new Car("Toyota Camry");
myCar.StartCar();
Console.WriteLine("Car object will be destroyed, Engine goes with it");
}
}
The output of the above code is −
Engine created: V6 Car created: Toyota Camry Starting Toyota Camry V6 engine started Car object will be destroyed, Engine goes with it
Aggregation
Aggregation represents a weaker "has-a" relationship where child objects can exist independently of the parent object. The parent uses existing child objects rather than creating them.
Example
using System;
public class Address {
private string street, city, state;
public Address(string street, string city, string state) {
this.street = street;
this.city = city;
this.state = state;
Console.WriteLine("Address created: " + street + ", " + city + ", " + state);
}
public string GetFullAddress() {
return street + ", " + city + ", " + state;
}
}
public class Employee {
private string name;
private Address address; // Aggregation
public Employee(string name, Address address) {
this.name = name;
this.address = address; // Employee uses existing Address
Console.WriteLine("Employee created: " + name);
}
public void DisplayInfo() {
Console.WriteLine("Employee: " + name);
Console.WriteLine("Address: " + address.GetFullAddress());
}
}
public class Program {
public static void Main() {
Address addr = new Address("123 Main St", "New York", "NY");
Employee emp = new Employee("John Smith", addr);
emp.DisplayInfo();
Console.WriteLine("Employee destroyed, but Address still exists");
}
}
The output of the above code is −
Address created: 123 Main St, New York, NY Employee created: John Smith Employee: John Smith Address: 123 Main St, New York, NY Employee destroyed, but Address still exists
Comparison
| Composition | Aggregation |
|---|---|
| Strong "part-of" relationship | Weak "has-a" relationship |
| Child cannot exist without parent | Child can exist independently |
| Parent creates child objects | Parent uses existing child objects |
| When parent dies, child dies too | Child survives parent's destruction |
| Example: Car and Engine | Example: Employee and Address |
Conclusion
Composition creates strong ownership where child objects are integral parts of the parent, while Aggregation creates loose associations where objects can exist independently. Choose composition when objects are fundamentally connected, and aggregation when objects have a flexible relationship.
