Composition - VishalBhanderi31/C-Sharp-OOPS GitHub Wiki

Composition is another kind of relationship between two classes that allows one to contain the other. Has-a relationship Example:- Car has an Engine.

Benefits

  • Code re-use
  • More flexibility
  • A means to loose-coupling

Examples

  • Dbmigrator requires logging
  • Installer requires logging

//Syntax

public class Installer { private Logger _logger; public Installer(Logger logger) { _logger = logger; } }