SOLID Principles In C# - Liskov Substitution Principle

homolibere

Nick

Posted on February 15, 2024

SOLID Principles In C# - Liskov Substitution Principle

C# SOLID Principles In C# - Liskov Substitution Principle with some code

In object-oriented programming (OOP), the SOLID principles are a set of guidelines that help developers design software that is easy to maintain, understand, and extend. One of these principles is the Liskov Substitution Principle (LSP). In this post, we will explore what the Liskov Substitution Principle is and how it can be implemented in C#.

The Liskov Substitution Principle states that objects of a superclass should be able to be replaced by objects of its subclasses without affecting the correctness of the program. In other words, a subclass should be able to substitute its superclass in any context without breaking the behavior expected from the superclass.

To illustrate this principle, let's consider an example where we have a base class called Shape and two subclasses called Circle and Rectangle. Each shape has a method called CalculateArea(), which calculates and returns its area.

public abstract class Shape
{
    public abstract double CalculateArea();
}

public class Circle : Shape
{
    public double Radius { get; set; }

    public override double CalculateArea()
    {
        return Math.PI * Math.Pow(Radius, 2);
    }
}

public class Rectangle : Shape
{
    public double Width { get; set; }
    public double Height { get; set; }

    public override double CalculateArea()
    {
        return Width * Height;
    }
}
Enter fullscreen mode Exit fullscreen mode

Now, let's say we have a function called PrintArea() that takes an instance of the Shape base class and prints its area. According to the Liskov Substitution Principle, we should be able to pass an instance of any subclass to this function without affecting its correctness.

public void PrintArea(Shape shape)
{
    Console.WriteLine("Area: " + shape.CalculateArea());
}
Enter fullscreen mode Exit fullscreen mode

This function delegates the responsibility of calculating the area to the respective subclass implementation via the CalculateArea() method.

Let's test this principle by creating instances of the Circle and Rectangle classes and passing them to the PrintArea() function.

Shape circle = new Circle { Radius = 5 };
Shape rectangle = new Rectangle { Width = 4, Height = 6 };

PrintArea(circle); // Output: Area: 78.53981633974483
PrintArea(rectangle); // Output: Area: 24
Enter fullscreen mode Exit fullscreen mode

As we can see, we were able to pass instances of both the Circle and Rectangle classes to the PrintArea() function without any issues. This demonstrates that the Liskov Substitution Principle has been followed, and the behavior of the base class has been preserved by its subclasses.

In conclusion, the Liskov Substitution Principle is an essential aspect of software design that ensures logical substitution of objects without affecting the correctness of the program. By adhering to this principle, we can create more maintainable and extensible code.

💖 💪 🙅 🚩
homolibere
Nick

Posted on February 15, 2024

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related

Demystifying Algorithms: Doubly Linked Lists
datastructures Demystifying Algorithms: Doubly Linked Lists

November 29, 2024

Demystifying Algorithms: Circular Linked Lists
datastructures Demystifying Algorithms: Circular Linked Lists

November 29, 2024

Demystifying Algorithms: Singly Linked Lists
datastructures Demystifying Algorithms: Singly Linked Lists

November 29, 2024

i18n e ASP.NET Core Web API
csharp i18n e ASP.NET Core Web API

November 28, 2024