Roadmap › OOP Concepts in C# › Module: Relationships and Design
Lessons in this course (15)
12. Association, aggregation and composition
Intermediate★ Must knowLesson 12 of 15
| Relationship | Meaning | Lifetime | Example |
|---|---|---|---|
| Association | "uses" / knows about | Independent | Teacher ↔ Student |
| Aggregation | "has-a", weak ownership | Part can exist without whole | Department has Employees |
| Composition | "owns-a", strong ownership | Part dies with whole | Order owns OrderLines; House owns Rooms |
| Inheritance | "is-a" | — | Car is a Vehicle |
csharp
public class Order // composition: lines are created and owned by the order
{
private readonly List<OrderLine> _lines = [];
public void Add(string sku, int qty) => _lines.Add(new OrderLine(sku, qty));
}
public class Department // aggregation: employees exist independently
{
public List<Employee> Members { get; } = [];
}