C# — Open-Closed-Principle — What really matters
An easy explanation of the Open-Closed-Principle in C#
Software should be readable, modular and maintainable. Unfortunately, in the course of development, but especially in the case of further developments, bad software design occurs again and again.
In the worst case, program errors occur in functionalities that have already been tested for a long time. Observing the SOLID principles of object-oriented software development can help to avoid these errors and keep program code maintainable. In particular, maintainability is of great importance, since software development is rarely done on the green field, but in most cases it is based on existing program code.
- The Open-Closed-Principle
- Use cases
- Examples
- Conclusion
The Open-Closed-Principle
Put simply: the open-closed principle describes that a class with its functions should be closed to changes to existing elements, but must be open to modifications.
The importance of the OCP is reflected in the maintainability and flexibility of the software. By following this principle, developers can make changes to the software without having to rewrite large amounts of code, which can save time and reduce the risk of introducing bugs.
Use cases
Basically, the principle is popular in several different areas. In order to better explain the idea behind it, I’ll go into more detail about inheritance and extension methods.
inheritance:
In inheritance, a child class inherits all of the properties (arrays and methods — that is, structure and behavior) of its parent class and adds its own individual properties or overrides methods of the parent class. The properties of the parent class do not have to be repeated in the specification of the child class. It is said that the son class is derived from the father class. So the principle of inheritance simply states that all properties and methods in the class that can be inherited from the superclass. The following graphic illustrates the principle of inheritance:

In this case, the OPC is used because the parent class already offers methods that do not have to be changed. The child classes inherit from this class and can also use the given methods. Additional methods can also be defined here.
Extension-Methods
Extension methods make it possible to extend existing types with new methods without having to change the definition of the original type. An extension method is a static method of a static class where the “this” (in C#) modifier is passed to the first parameter.

“Extension methods are a special kind of static method but can be called on objects like an instance method. So, an extension method can be used in the same way as normal instance methods.” (Dhananjay, 2016)
If you want to learn more about Extension-Methods, please read my article: Extension Methods in C# explained (following soon)
Examples
The following examples are for illustrative purposes only and were created by ChatGPT
Here is an example of how the open-closed principle can be applied in C#:
Suppose we have a class that represents a simple shape, such as a circle or a rectangle. We might write the class as follows:

This class has a virtual method called Area() that returns the area of the shape. We can then create subclasses for specific types of shapes, such as circles and rectangles, that override the Area() method to provide the correct implementation:

Now suppose we want to add a new type of shape to our application, such as a triangle. If we followed the open-closed principle, we would not modify the Shape class to add a new method for calculating the area of a triangle. Instead, we would create a new subclass of Shape and override the Area() method to provide the correct implementation:

By following this approach, we have added the ability to calculate the area of a triangle to our application without modifying the existing code. This makes our code more flexible and easier to maintain.
Conclusion
In summary, the open-closed principle is a software design principle that states that software entities should be open for extension but closed for modification. In practical application, the principle is used wherever possible. To illustrate how it works, inheritance and extension methods were chosen as an example. By following this principle, developers can add new functionality to their software without having to rewrite large amounts of code, which helps to promote software maintainability and flexibility. All in all, as a developer you should always stick to this principle and ensure that your software is processed properly.
Thanks for reading and if you like this article, please follow me on Medium! :)
Please check out my other articles:
- LINQ — How to avoid nested loops in C#
- .Net C# — Clean Architecture & Dependency-Inversion-Principle
- Programming paradigms — a brief introduction
- C# — Object-Oriented Programming (OOP)
- C# — Single-Responsibility easily explained
- Top 8 tips to improve your motivation as programmer
- A brief insight into Networks
- Send & Receive — The 7-Layer-OSI-Model
- 7-Layer Network Protocols easily explained
