Introduction
Apart from programming, a lot of my spare time sat at the computer is spent reading group, blog postings, etc from other developers. One particular posting that caught my eye recently provoked a lot of response and mixed answers to a question posed by a poster. This question was, 'What is the difference between composition and aggregation and how would I express it in my programs'?
Reading the responses to the post, I had a mixed reaction, many of the responses reflected my understanding of the difference, others turned my understanding right around and explained composition as my understanding of aggregation.
This short article will put forward my understanding of composition and aggregation and how I would express it in C# code.
Composition
As we know, inheritance gives us an 'is-a' relationship. To make the understanding of composition easier, we can say that composition gives us a 'part-of' relationship. Composition is shown on a UML diagram as a filled diamond (see Figure 1).

If we were going to model a car, it would make sense to say that an engine is part-of a car. Within composition, the lifetime of the part (Engine) is managed by the whole (Car), in other words, when Car is destroyed, Engine is destroyed along with it. So how do we express this in C#?
public class Engine
{
. . .
}
public class Car
{
Engine e = new Engine();
.......
}
As you can see in the example code above, Car manages the lifetime of Engine.
Aggregation
If inheritance gives us 'is-a' and composition gives us 'part-of', we could argue that aggregation gives us a 'has-a' relationship. Within aggregation, the lifetime of the part is not managed by the whole. To make this clearer, we need an example. For the past 12+ months I have been involved with the implementation of a CRM system, so I am going to use part of this as an example.
The CRM system has a database of customers and a separate database that holds all addresses within a geographic area. Aggregation would make sense in this situation, as a Customer 'has-a' Address. It wouldn't make sense to say that an Address is 'part-of' the Customer, because it isn't. Consider it this way, if the customer ceases to exist, does the address? I would argue that it does not cease to exist. Aggregation is shown on a UML diagram as an unfilled diamond (see Figure 2).

So how do we express the concept of aggregation in C#? Well, it's a little different to composition. Consider the following code:
public class Address
{
. . .
}
public class Person
{
private Address address;
public Person(Address address)
{
this.address = address;
}
. . .
}
Person would then be used as follows:
Address address = new Address();
Person person = new Person(address);
or
Person person = new Person( new Address() );
As you can see, Person does not manage the lifetime of Address. If Person is destroyed, the Address still exists. This scenario does map quite nicely to the real world.
Conclusion
As I said at the beginning of the article, this is my take on composition and aggregation. Making the decision on whether to use composition or aggregation should not be a tricky. When object modelling, it should be a matter of saying is this 'part-of' or does it 'have-a'?
Joren WoutersPosted Oct 20, 2017, 6:53 AM
Is is possible to also that the constructor method of Person, doesn't have Address as Parameter, but (some way) there is still an aggregation relation? So another way to do it? Asking for a particular project...
Zahid MehmoodPosted Aug 17, 2017, 3:26 AM
You did a great job. Simply explained a very complex concept to me. Bravo
rahul rathorePosted Sep 17, 2016, 10:11 AM
Nice explanation.
Satish Kumar YadavPosted Feb 1, 2016, 2:05 AM
Thanq
C# CornerPosted Aug 19, 2015, 2:46 AM
In simple words you explain one of most confused concept. I really appreciate your effort and skills. Keep writing and spreading knowledged.
Ahmed DawodPosted May 24, 2015, 5:25 PM
This is a very good article to clear these concepts out
Prakash TripathiPosted May 2, 2015, 7:44 AM
Nice article. just to add there are two more related concepts called Inheritance and Association. Below article explains them nicely. http://www.codeproject.com/Articles/330447/Understanding-Association-Aggregation-and-Composit
Sams ManchiliPosted Apr 20, 2015, 1:49 PM
Hallo All, I am little confused. When it was explained that "has-a" is aggregation "Person has-a Address" means Address object still exists even that "Person" does not exists. Address is passed to Person while creating. Based on this theory, isn't it Adapter "has-a" Connection and Builder "has-a"n Adapter? I have zero knowledge on .NET.
ALI MOHAMMAD BAHMANYARPosted Jan 19, 2015, 4:55 AM
very nice thank u man
mohammad samirPosted Feb 27, 2013, 7:55 PM
very nice.. thank u
John OrtizPosted Jun 10, 2012, 6:28 PM
It is so clear. Thanks for this article. So long!
Seyed Ahmad ParkhidPosted May 25, 2012, 11:36 AM
Very Very Nice Explanation , Good Job Man !
Muhammad NaeemPosted Dec 5, 2011, 2:50 PM
Well explained....Good job.
aditya bokadePosted Feb 24, 2011, 9:18 AM
I am a new to .NET and I use to get confused always since in basic C++ we always wrote something like this: Person p= new Person(); or sometimes int age=25; Person p = new Person(); So many times I used to get confused with why the hell most of the times we use Some other object in the constructor such as : Person p=new Person(25); Doctor D=new Doctor(p); <---------------- Here was my confusion. I knew the concept but they were not fairly clear. Today After reading this article I understood that its the part of Aggregation!!!!!!!!! And I also clearly understand that D is the CONTAINER object of P. So I think I should define Composition & Aggregation like this: Composition : Occurs when Container object manages the lifetime of Containing object. Both object gets disposed when Container's life time ends. Aggregation : Occurs when Container object DOES NOT manages the lifetime of Containing object. Containing object remains alive till its life time is ended although Containing object is not alive anymore. [email protected] Case study is here about ADO.NET: SqlConnection thisConnection = new SqlConnection(@"Server=(local)\sqlexpress;Integrated Security=True;" +"Database=northwind"); SqlDataAdapter thisAdapter = new SqlDataAdapter( "SELECT CustomerID, CompanyName FROM Customers", thisConnection); SqlCommandBuilder thisBuilder = new SqlCommandBuilder(thisAdapter); Here Aggregation is like this : Connection HAS An adapter ; Adapter HAS A command builder. and both connection and adapter won't be disposed even if command builder is disposed :) Please help me improving the definition. Comments are most welcome. Regards, Aditya N Bokade
lliPosted Jun 24, 2010, 6:01 AM
Can we consider a inner class like a composition ? namespace MyCars { public class Car { // Aggregation uses instances of objects created outside of // this class protected Door _frontRight; protected Door _frontLeft; protected Door _rearRight; protected Door _rearLeft; // inner classes used to create objects that are intrinsically // linked to the class Car protected class Engine { public int _horsePower; } protected class Battery { public int _voltage; } // Composition uses instances of objects that are created as // part of this object protected Engine _theEngine; protected Battery _theBattery; } public class Door { public int _position; } } namespace MyCars { // Inherit from class Car public class FordCapri : Car { public FordCapri() { _theEngine._horsePower = 2000; } } }
Basant KumarPosted Jan 19, 2010, 11:30 PM
Nice Article, This clears the concept of Aggregation Vs Composition from implementation point of view. Its like, if we are injecting one object (Address) into another object (Person) via some means, then it is definitely an Aggregation. But when the object (Engine) is instantiated in the containg class (Car), its a Composition.
zakir sajibPosted Sep 23, 2009, 4:13 PM
nice explanation, helped me to clear my concept of composition and aggregation. but i think finding a good example of aggregation is difficult. and this is a weaker type of relationship than composition. I found one example of aggregation. in UK, different type of migrants live, one is british citizen, another british citzen with dual citizenship, temporary residence, indefinite residence. So if UK is destroyed, those citizen who have dual citizenship to other countries may still exist. In same way, temporary residence and indefinite residence can also exist with out UK, because they belong to other nations. I think this above scenario could be an example of aggregation relationship. What do you think?
Deepak RaghavanPosted Jul 28, 2007, 11:57 PM
The constructor of the Person class is mistyped as Address. Just an FYI...Good job on the article.
Mike GoldeditedPosted Jul 27, 2007, 6:37 PMEdited Jul 27, 2007, 6:38 PM
One thing you might want to mention is that composition is often referred to as "strong aggregation", where "weak aggregation" is the aggregation you described. All the relationships you described can be grouped under the general term of "associations"