CSharpMaster – How to Implement Open Closed Principle in C# Example (SOLID)

Open Closed Principle in C# Example : In the world of software development, adhering to solid design principles is crucial for building robust, maintainable, and scalable systems. One of the five SOLID principles is the Open Closed Principle in c# (OCP).

This principle asserts that software components (such as classes, modules, functions, etc.) should be designed to allow their behavior to be extended without requiring modifications to their existing code. In this blog post, we’ll learn how to implement the Open Closed Principle in C# with practical examples. Here we will get good understanding of OCP and how it can improve your code quality, maintainability, and scalability.

Understanding the Open Closed Principle

The Open/Closed Principle was introduced by Bertrand Meyer in 1988. The essence of this principle is that once a class has been developed and tested, it should not be modified. Instead, the class should be extended to meet new requirements.

Open for Extension: This means that the functionality of the module can be enhanced. For example, we should be able to add new functionality.

Closed for Modification: This means that the source code of the module is not changed. Therefore, existing functionalities are not broken when you add new features.

Why Follow the c# Open Closed Principle?

  1. Maintainability: Since existing code is not modified, the risk of introducing new bugs is minimized.
  2. Scalability: New functionality can be added without altering existing code, making it easier to scale the application.
  3. Readability: Code that follows OCP tends to be cleaner and easier to understand.
  4. Testability: Tests for the existing functionality remain valid since the original code remains unchanged.

Implementing Open Closed Principle in C#

To implement OCP in C#, we generally use interfaces or abstract classes and inheritance. Let’s walk through a concrete example to illustrate this principle in action.

Example Scenario: Payment Processing System

Imagine we are developing a payment processing system that initially supports only credit card payments. Later, we want to extend it to support PayPal payments without modifying the existing credit card payment processing code.

Step 1: Define an Interface

First, we define an interface IPaymentProcessor which will be implemented by different payment processors.

How to Implement Open Closed Principle in C# Example
How to Implement Open Closed Principle in C# Example
Step 2: Implement the Credit Card Payment Processor

Next, we implement the CreditCardPaymentProcessor class that processes credit card payments.

Open Closed Principle in C# Example
Open Closed Principle in C# Example
Step 3: Extend Functionality with PayPal Payment Processor

To extend the functionality to support PayPal payments, we create a new class PayPalPaymentProcessor that implements the IPaymentProcessor interface.

Open Closed Principle in C#
Open Closed Principle in C#
Step 4: Use Dependency Injection to Manage Payment Processors

Using Dependency Injection (DI), we can inject the appropriate payment processor without modifying the client code.

open closed principle example c#
open closed principle example c#
Step 5: Configure Dependency Injection

Finally, we configure the DI container to resolve the appropriate payment processor.

c# open closed principle
c# open closed principle

In this example, we’ve demonstrated how to adhere to the Open Closed Principle by creating an extensible system where new payment methods can be added without modifying existing code.

Learn our best article related to boxing and unboxing in c# example.

Benefits of Following the Open Closed Principle in C#

  1. Enhanced Flexibility: By designing your system to be open for extension, you can easily add new features or support new requirements without touching the existing codebase.
  2. Improved Code Quality: Encapsulation and the use of interfaces or abstract classes often lead to more modular and understandable code.
  3. Reduced Risk: Minimizing changes to existing code reduces the likelihood of introducing bugs, making your system more reliable.

Common Pitfalls and How to Avoid Them

While the Open/Closed Principle is powerful, there are common pitfalls that developers may encounter:

  1. Over-Engineering: Trying to anticipate every possible extension can lead to overly complex and bloated code. Aim for a balance by focusing on immediate needs and foreseeable extensions.
  2. Insufficient Abstraction: If abstractions are too specific, they may not adequately support future extensions. Strive for abstractions that are flexible enough to accommodate likely changes.
  3. Violation of Other SOLID Principles: Ensure that adhering to OCP does not cause you to violate other SOLID principles, such as the Single Responsibility Principle (SRP).


The Open Closed Principle is a foundational concept in object-oriented design that, when properly implemented, can significantly improve the maintainability and scalability of your codebase. By using interfaces, abstract classes, and dependency injection, you can create systems that are both flexible and robust.

In this chsarpmaster’s blog post, we provided a step-by-step guide on how to implement the Open Closed Principle in C# with a practical payment processing example. By following these guidelines, you can enhance your C# applications, making them easier to maintain and extend over time.

Remember, the key to mastering OCP is to practice designing your systems with extension in mind. Happy coding!

By following these principles and practices, you ensure that your code remains robust, maintainable, and adaptable to future requirements. This approach not only helps in creating high-quality software but also in gaining a deeper understanding of effective software c# design patterns.

Leave a Reply

Your email address will not be published. Required fields are marked *