C Sharp Aggregation

C# Aggregation (HAS-A Relationship)

The process of defining a class as an entity reference by another class is called aggregation in C#. Thus it can be used to reuse a class. It represents a HAS-A relationship as a form of association.

Example:

using System;  
public class Details  
{  
    public int age;  
    public string city;  
    public float marks;  
    public Details(int age, string city, float marks)  
    {  
        this.age = age;  
        this.city = city;  
        this.marks = marks;  
    }  
}  
   public class Student  
    {  
       public int id;  
       public string name;  
       public Details details;
       public Student(int id, string name, Details details)  
       {  
           this.id = id;  
           this.name = name;  
           this.details = details;  
       }  
       public void display()  
       {  
           Console.WriteLine(id + " " + name + " " +   
             details.age + " " + details.city + " " + details.marks);  
       }  
   }  
   public class values  
   {  
        public static void Main(string[] args)  
        {  
            Details d1 = new Details(23,"Lucknow", 90);  
            Student s1 = new Student(1,"Vishal Gupta",d1);  
            s1.display();  
        }  
    }

Output:

Explanation:

In the above example, we are displaying the use and behavior of aggregation. Here, the Student class has the reference of Details class as a data member. Thus the Student class can reuse the members of the Details class.

Please Share