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
Association, Composition and Aggregation in C#
In C#, object-oriented programming relies on relationships between classes to model real-world scenarios. Three fundamental types of relationships are Association, Composition, and Aggregation. These relationships define how objects interact and depend on each other.
Association in C#
Association defines a relationship between objects where they use each other but maintain their independence. It represents a "uses-a" relationship and can be one-to-one, one-to-many, many-to-one, or many-to-many.
Example
using System;
using System.Collections.Generic;
class Employee {
public string Name { get; set; }
public Employee(string name) { Name = name; }
}
class Project {
public string ProjectName { get; set; }
public List<Employee> Employees { get; set; }
public Project(string projectName) {
ProjectName = projectName;
Employees = new List<Employee>();
}
public void AssignEmployee(Employee emp) {
Employees.Add(emp);
}
public void ShowProject() {
Console.WriteLine("Project: " + ProjectName);
foreach(Employee emp in Employees) {
Console.WriteLine("Employee: " + emp.Name);
}
}
}
class Program {
public static void Main() {
Employee emp1 = new Employee("John");
Employee emp2 = new Employee("Alice");
Project project1 = new Project("Web Development");
project1.AssignEmployee(emp1);
project1.AssignEmployee(emp2);
project1.ShowProject();
}
}
The output of the above code is −
Project: Web Development Employee: John Employee: Alice
Aggregation in C#
Aggregation represents a "has-a" relationship with weak ownership. The child object can exist independently of the parent object. It's a specialized form of association where one class owns another, but both can exist separately.
Example
using System;
using System.Collections.Generic;
class Employee {
public string Name { get; set; }
public Employee(string name) { Name = name; }
}
class Department {
public string DeptName { get; set; }
public List<Employee> Employees { get; set; }
public Department(string deptName) {
DeptName = deptName;
Employees = new List<Employee>();
}
public void AddEmployee(Employee emp) {
Employees.Add(emp);
}
public void ShowDepartment() {
Console.WriteLine("Department: " + DeptName);
foreach(Employee emp in Employees) {
Console.WriteLine("Employee: " + emp.Name);
}
}
}
class Program {
public static void Main() {
Employee emp1 = new Employee("John");
Employee emp2 = new Employee("Sarah");
Department dept = new Department("IT");
dept.AddEmployee(emp1);
dept.AddEmployee(emp2);
dept.ShowDepartment();
Console.WriteLine("\nEmployees exist independently:");
Console.WriteLine(emp1.Name + " still exists");
Console.WriteLine(emp2.Name + " still exists");
}
}
The output of the above code is −
Department: IT Employee: John Employee: Sarah Employees exist independently: John still exists Sarah still exists
Composition in C#
Composition represents a "part-of" relationship with strong ownership. If the parent object is destroyed, the child objects are also destroyed. The child cannot exist without the parent, making it the strongest form of association.
Example
using System;
class Engine {
public string Type { get; set; }
public Engine(string type) { Type = type; }
public void Start() {
Console.WriteLine(Type + " engine started");
}
}
class Car {
public string Model { get; set; }
private Engine engine;
public Car(string model) {
Model = model;
engine = new Engine("V6");
}
public void StartCar() {
Console.WriteLine("Starting " + Model);
engine.Start();
}
}
class Program {
public static void Main() {
Car car = new Car("Toyota Camry");
car.StartCar();
car = null;
GC.Collect();
Console.WriteLine("Car destroyed - Engine is also destroyed");
}
}
The output of the above code is −
Starting Toyota Camry V6 engine started Car destroyed - Engine is also destroyed
Comparison
| Relationship | Type | Dependency | Example |
|---|---|---|---|
| Association | "uses-a" | Independent objects | Employee works on Project |
| Aggregation | "has-a" | Child can exist without parent | Department has Employees |
| Composition | "part-of" | Child cannot exist without parent | Car has Engine |
Conclusion
Understanding Association, Aggregation, and Composition helps design better object-oriented systems in C#. Association provides loose coupling, Aggregation offers moderate coupling with shared ownership, while Composition ensures tight coupling with exclusive ownership where child objects depend entirely on their parents.
