Skip to content
Tutorials Center
My learning

Topics

C# Reference ยท Classes and Object-Oriented Concepts

SOLID in C#

Reference for the SOLID principles in C#: single responsibility, open/closed, Liskov substitution, interface segregation and dependency inversion, with runnable examples.

SOLID is a set of five design guidelines for object-oriented code. They are not language rules, and the compiler does not enforce them. They describe designs that tend to be easy to change and easy to test: Single responsibility, Open/closed, Liskov substitution, Interface segregation and Dependency inversion.

C# gives you the tools for each: small classes and interfaces, polymorphism through virtual and interfaces, generics with constraints, and dependency injection. Treat the principles as questions to ask during design, not as goals to maximize. Splitting every class into tiny pieces for its own sake makes code harder to follow.

C#
// Depend on an abstraction, not on a concrete class (D)
public class OrderService(INotifier notifier)
{
    public void Place() => notifier.Send("Order placed");
}

Try it

C# Single responsibility and open/closed

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.

C# Liskov substitution, interface segregation, dependency inversion

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.

The five principles

Letter Principle Practical question
S Single responsibility Does this class have more than one reason to change?
O Open/closed Can I add behavior by adding code instead of editing working code?
L Liskov substitution Can every subtype be used where the base type is expected without surprises?
I Interface segregation Are callers forced to depend on members they never use?
D Dependency inversion Does high-level logic depend on abstractions rather than concrete details?

Usage notes

  • Single responsibility is about reasons to change, not about the number of methods. A class that formats, validates and saves data changes for three different reasons.
  • Open/closed is typically achieved with an interface and several implementations, as in the first example. Strategy objects, delegates and switch expressions over a closed set are all reasonable.
  • Liskov substitution is a contract: a subtype may not demand more (stricter preconditions), promise less, or throw exceptions the base type never throws. A virtual method override that throws NotSupportedException is a warning sign.
  • Interface segregation means several small interfaces beat one large one. A class can implement many; IReader and IWriter above let read-only code stay read-only.
  • Dependency inversion is a design rule; dependency injection is a technique that follows it. See Dependency injection.
  • Composition over inheritance supports most of these principles. Prefer injecting a collaborator over deriving from a base class.
  • Do not over-apply. An interface with a single implementation and no test need is often noise. Introduce abstractions where change or testing actually happens.
  • Sealed by default is a good starting point: unsealed classes carry an implicit promise that subclasses will behave.