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 demoing the same.
Then, I have also created two other dependent classes as shown below.
Once, the class gets created, It’s time to feed the value via constructor. Hence, below is the finished code for the same.
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
And, when i run the program, it will produce the following output.
However, I can modify the main program to add more info to this via aggregation as shown below.
With the above change in place, it will display the following info.
Therefore, in a nutshell, Aggregation is the stuff which can be added at later point of time to the object but, composition got constructed with object creation itself.
4,733 total views, 2 views today