What is composition in C#?

Composition in C# is a design principle where one class contains an instance of another class as a private member, creating a "has-a" relationship. In composition, the contained object (child) cannot exist independently of the container object (parent). When the parent object is destroyed, the child object is also destroyed, establishing a strong ownership relationship.

Composition represents a part-of relationship, which is a special type of association that implies strong coupling between objects.

Syntax

Following is the basic syntax for implementing composition in C# −

public class ContainedClass {
   // properties and methods
}

public class ContainerClass {
   private ContainedClass obj = new ContainedClass();
   // other members
}

Composition Relationship Car (Container) Engine (Contained) has-a Strong Ownership Engine cannot exist without Car When Car is destroyed, Engine is also destroyed

Using Composition with Car and Engine

Example

using System;

public class Engine {
   public string Type { get; set; }
   public int Horsepower { get; set; }
   
   public Engine(string type, int horsepower) {
      Type = type;
      Horsepower = horsepower;
      Console.WriteLine($"Engine created: {Type}, {Horsepower} HP");
   }
   
   public void Start() {
      Console.WriteLine($"{Type} engine started!");
   }
}

public class Car {
   private Engine engine;
   public string Model { get; set; }
   
   public Car(string model, string engineType, int horsepower) {
      Model = model;
      engine = new Engine(engineType, horsepower);
      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", "V6", 268);
      myCar.StartCar();
      Console.WriteLine("Car object going out of scope...");
   }
}

The output of the above code is −

Engine created: V6, 268 HP
Car created: Toyota Camry
Starting Toyota Camry...
V6 engine started!
Car object going out of scope...

Composition vs Aggregation

Composition Aggregation
Strong "part-of" relationship Weak "has-a" relationship
Child cannot exist without parent Child can exist independently
Parent creates and owns child object Parent uses existing child object
Example: Car and Engine Example: Department and Employee

Using Composition with Multiple Components

Example

using System;

public class CPU {
   public string Model { get; set; }
   public CPU(string model) { Model = model; }
   public void Process() {
      Console.WriteLine($"{Model} is processing data");
   }
}

public class RAM {
   public int Size { get; set; }
   public RAM(int size) { Size = size; }
   public void LoadData() {
      Console.WriteLine($"{Size}GB RAM is loading data");
   }
}

public class Computer {
   private CPU cpu;
   private RAM ram;
   public string Brand { get; set; }
   
   public Computer(string brand, string cpuModel, int ramSize) {
      Brand = brand;
      cpu = new CPU(cpuModel);
      ram = new RAM(ramSize);
      Console.WriteLine($"{Brand} computer assembled");
   }
   
   public void Boot() {
      Console.WriteLine($"Booting {Brand} computer...");
      ram.LoadData();
      cpu.Process();
      Console.WriteLine("Computer ready!");
   }
}

public class Program {
   public static void Main() {
      Computer pc = new Computer("Dell", "Intel i7", 16);
      pc.Boot();
   }
}

The output of the above code is −

Dell computer assembled
Booting Dell computer...
16GB RAM is loading data
Intel i7 is processing data
Computer ready!

Conclusion

Composition in C# creates a strong ownership relationship where the contained object cannot exist independently of its container. This design principle promotes encapsulation and provides better control over object lifecycles, making it ideal for scenarios where components are integral parts of a whole system.

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

636 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements