Table of contents
C# is an object-oriented programming language, which means it relies heavily on classes to structure and organize code. Classes in C# can have a variety of relationships with each other, each serving a different purpose in code design. In this article, we will explore the different relationships between classes in C# and provide examples of each.
Inheritance
Inheritance is a relationship where one class is derived from another. The derived class, also known as the subclass, inherits properties and methods from the parent class, also known as the base class. This allows for code reuse and abstraction, as the subclass can leverage the functionality of the parent class.
Here's an example of inheritance in C#:
public class Animal
{
public string Name { get; set; }
public virtual void MakeSound()
{
Console.WriteLine("Some generic animal sound.");
}
}
public class Dog : Animal
{
public override void MakeSound()
{
Console.WriteLine("Woof!");
}
}
In this example, the Dog
class is derived from the Animal
class. The Dog
class inherits the Name
property from the Animal
class and overrides the MakeSound
method to provide its own implementation.
Composition
Composition is a relationship where a class is composed of one or more instances of another class. The composed class can be thought of as a component of the parent class. This allows for more complex objects to be built from simpler ones.
Here's an example of composition in C#:
public class Car
{
private Engine _engine;
public Car()
{
_engine = new Engine();
}
public void Start()
{
_engine.Start();
}
}
public class Engine
{
public void Start()
{
Console.WriteLine("Engine started.");
}
}
In this example, the Car
class is composed of an instance of the Engine
class. The Car
class has a Start
method, which in turn calls the Start
method of the Engine
instance.
Aggregation
Aggregation is a relationship where a class contains one or more instances of another class, but the composed class has a life of its own and can exist outside of the parent class. This is similar to composition, but the composed class is not necessarily dependent on the parent class.
Here's an example of aggregation in C#:
public class Company
{
public List<Employee> Employees { get; set; }
}
public class Employee
{
public string Name { get; set; }
}
In this example, the Company
class has a list of Employee
instances. The Employee
class can exist independently of the Company
class, but the Company
class can aggregate multiple Employee
instances.
Association
Association is a relationship where two classes are related to each other, but neither class is a component of the other. This can be a simple or complex relationship, depending on the needs of the code.
Here's an example of association in C#:
public class Student
{
public string Name { get; set; }
}
public class Course
{
public string Name { get; set; }
public List<Student> Students { get; set; }
}
In this example, the Student
class and Course
class are associated with each other. A Course
has a list of Student
instances, but neither class is a component of the other.
In conclusion, understanding the different relationships between classes in C# is important for designing effective and efficient code. By using inheritance, composition, aggregation, and association appropriately