In today's discussion, we will discuss Aggregation and Composition in detail using C#. So, let's get started. Let me go ahead and create simple console App for demo purpose.

  1. namespace CSharpStuffs
  2. {
  3. public class Patient
  4. {
  5. public string PatientName { get; set; }
  6. public string AdmissionDate { get; set; }
  7. public string ReleaseDate { get; set; }
  8. public DoctorInfo DoctorInfo { get; set; }
  9. public LifeInsurance Insurance { get; set; }
  10. }
  11. }
  1. namespace CSharpStuffs
  2. {
  3. public class LifeInsurance
  4. {
  5. public string PolicyName;
  6. public string CompanyName;
  7. }
  8. }
Once, the class gets created, It's time to feed the value via constructor. Hence, the following is the final code for the same.
  1. namespace CSharpStuffs
  2. {
  3. public class Patient
  4. {
  5. public string PatientName { get; set; }
  6. public string AdmissionDate { get; set; }
  7. public string ReleaseDate { get; set; }
  8. public DoctorInfo DoctorInfo { get; set; }
  9. public LifeInsurance Insurance { get; set; }
  10. public Patient(string patientName,string admissionDate,string releaseDate,string doctorName,string doctorSpeciality, string workingAt)
  11. {
  12. PatientName = patientName;
  13. AdmissionDate = admissionDate;
  14. ReleaseDate = releaseDate;
  15. //Composition comes into picture now
  16. //It also means when we construct Patient, we also constructed DoctorInfo
  17. //Hence, scope of Doctorinfo is dependent on Patient
  18. DoctorInfo = new DoctorInfo
  19. {
  20. DoctorName = doctorName,
  21. DoctorSpeciality = doctorSpeciality,
  22. WorkingAt = workingAt
  23. };
  24. }
  25. //Overrideenn ToString() for convience
  26. public override string ToString()
  27. {
  28. string result = PatientName + " " +
  29. AdmissionDate + " " +
  30. ReleaseDate + " ";
  31. //Filling Other objects
  32. if (DoctorInfo != null)
  33. {
  34. result += DoctorInfo.DoctorName + " "
  35. + DoctorInfo.DoctorSpeciality + " "
  36. + DoctorInfo.WorkingAt;
  37. }
  38. if (Insurance != null)
  39. {
  40. result += " "+ Insurance.PolicyName + " "
  41. + Insurance.CompanyName;
  42. }
  43. return result;
  44. }
  45. }
  46. }
You will also notice that here, I have used Composition concept. Composition is tied with actual object which is patient here and hence DoctorInfo also got constructed.

Now, main program looks like the following:
  1. using System;
  2. namespace CSharpStuffs
  3. {
  4. class Program
  5. {
  6. static void Main(string[] args)
  7. {
  8. Patient patient = new Patient("Tim","12-08-2015","20-08-2015","Cook","Physician","Fortis");
  9. Console.WriteLine(patient.ToString());
  10. Console.ReadLine();
  11. }
  12. }
  13. }
And, when I run the program, it will produce the following output.

run

However, I can modify the main program to add more info to this via aggregation as shown below.
  1. using System;
  2. namespace CSharpStuffs
  3. {
  4. class Program
  5. {
  6. static void Main(string[] args)
  7. {
  8. Patient patient = new Patient("Tim","12-08-2015","20-08-2015","Cook","Physician","Fortis");
  9. Console.WriteLine(patient.ToString());
  10. //Now, we can add insurance by aggregation
  11. patient.Insurance = new LifeInsurance
  12. {
  13. CompanyName = "HDFC",
  14. PolicyName = "LIFE-AXA"
  15. };
  16. Console.WriteLine(patient.ToString());
  17. Console.ReadLine();
  18. }
  19. }
  20. }
With the above change in place, it will display the following info.

display

Therefore, in a nutshell, Aggregation is the stuff which can be added at later point of time to the object, but composition gets constructed with object creation itself.

Thanks!