Curriculum
Aggregation is a relationship between two classes in object-oriented programming (OOP) where one class contains a reference to another class, but the contained class can exist independently of the containing class. In other words, the contained class is not a part of the containing class, but rather a separate entity that can be shared among multiple containing classes.
In C#, aggregation is implemented using reference variables. The containing class holds a reference to an instance of the contained class, but the contained class is not created or destroyed with the containing class. Instead, the contained class is created and destroyed separately, and can be shared among multiple containing classes.
Here is an example of aggregation in C#:
class Address
{
public string Street { get; set; }
public string City { get; set; }
public string State { get; set; }
public string ZipCode { get; set; }
}
class Person
{
public string Name { get; set; }
public Address Address { get; set; }
}
In this example, the Person class contains a reference to an instance of the Address class, which represents a postal address. The Person class does not create or destroy the Address object, but rather holds a reference to an existing Address object. This allows multiple Person objects to share the same Address object, and allows the Address object to exist independently of any Person objects.
To use aggregation in C#, we create instances of the contained class separately and then pass them to the containing class as parameters, or create them within the containing class using the new keyword. Here is an example:
Address address = new Address
{
Street = "123 Main St",
City = "Anytown",
State = "CA",
ZipCode = "12345"
};
Person person1 = new Person
{
Name = "John Doe",
Address = address
};
Person person2 = new Person
{
Name = "Jane Smith",
Address = address
};
Aggregation is a relationship between two classes in object-oriented programming (OOP) where one class contains a reference to another class, but the contained class can exist independently of the containing class. In other words, the contained class is not a part of the containing class, but rather a separate entity that can be shared among multiple containing classes.
In C#, aggregation is implemented using reference variables. The containing class holds a reference to an instance of the contained class, but the contained class is not created or destroyed with the containing class. Instead, the contained class is created and destroyed separately, and can be shared among multiple containing classes.
Here is an example of aggregation in C#:
class Address
{
public string Street { get; set; }
public string City { get; set; }
public string State { get; set; }
public string ZipCode { get; set; }
}
class Person
{
public string Name { get; set; }
public Address Address { get; set; }
}
In this example, the Person class contains a reference to an instance of the Address class, which represents a postal address. The Person class does not create or destroy the Address object, but rather holds a reference to an existing Address object. This allows multiple Person objects to share the same Address object, and allows the Address object to exist independently of any Person objects.
To use aggregation in C#, we create instances of the contained class separately and then pass them to the containing class as parameters, or create them within the containing class using the new keyword. Here is an example:
Address address = new Address
{
Street = "123 Main St",
City = "Anytown",
State = "CA",
ZipCode = "12345"
};
Person person1 = new Person
{
Name = "John Doe",
Address = address
};
Person person2 = new Person
{
Name = "Jane Smith",
Address = address
};
In this code, we create an instance of the Address class and set its properties to represent a postal address. We then create two instances of the Person class and set their Name and Address properties. Both Person objects reference the same Address object, which allows them to share the same postal address.
In summary, aggregation is a relationship between two classes in C# where one class contains a reference to another class, but the contained class can exist independently of the containing class. Aggregation allows us to reuse existing objects and create more modular and flexible code. To use aggregation in C#, we create instances of the contained class separately and pass them to the containing class as parameters, or create them within the containing class using the new keyword.