---
title: "Explain SOLID principles with real-life .NET examples."  
description: "Here's a clear breakdown of the SOLID principles with real-life C# / .NET examples"  
author: "Ravi Vishwakarma"  
published: 2025-06-15  
updated: 2025-12-16  
canonical: https://www.mindstick.com/articles/339591/explain-solid-principles-with-real-life-dot-net-examples  
category: "c#"  
tags: ["c#"]  
reading_time: 4 minutes  

---

# Explain SOLID principles with real-life .NET examples.

Absolutely! Here's a clear breakdown of the **[SOLID principles](https://answers.mindstick.com/qa/49599/what-is-solid-principles-in-c-sharp)** with **real-life C# / .NET examples**

## S — [Single Responsibility Principle (SRP)](https://www.mindstick.com/articles/33658/solid-principles-in-software-development-area)

**A class should have only [one reason](https://answers.mindstick.com/qa/42672/what-was-one-reason-for-the-failure-of-british-troops-in-the-south) to change.**

> Do **one thing**, and do it well.

### Bad Example:

```cs
public class InvoiceService
{
    public void GenerateInvoice() { /* ... */ }
    public void SaveToDatabase() { /* ... */ }
    public void SendEmail() { /* ... */ } // too many responsibilities
}
```

### Good Example:

```cs
public class InvoiceGenerator { public void Generate() { } }
public class InvoiceRepository { public void Save() { } }
public class EmailService { public void SendEmail() { } }
```

## O — [Open/Closed Principle (OCP)](https://medium.com/swift-india/solid-principles-part-2-open-closed-principle-bf0514b824ca)

**Software entities should be open for extension, but closed for modification.**

> Extend behavior **without changing existing code**.

### Bad Example:

```cs
public class Report
{
    public void GenerateReport(string type)
    {
        if (type == "PDF") { /* PDF logic */ }
        else if (type == "Excel") { /* Excel logic */ }
    }
}
```

### Good Example (using abstraction):

```cs
public interface IReportGenerator { void Generate(); }

public class PdfReport : IReportGenerator { public void Generate() { } }
public class ExcelReport : IReportGenerator { public void Generate() { } }

public class ReportService
{
    public void GenerateReport(IReportGenerator generator) => generator.Generate();
}
```

![Explain SOLID principles with real-life .NET examples.](https://www.mindstick.com/mindstickarticle/be354e4f-1dfd-4f9c-b31a-78671dceb913/images/e34519bb-3e2b-4cca-a63b-dd4c520ba3b9.png)

## L — [Liskov Substitution Principle (LSP)](https://www.mindstick.com/interview/34041/understanding-solid-principles-in-object-oriented-design)

**Objects of a superclass should be replaceable with objects of its subclasses without breaking the application.**

> A [derived class](https://answers.mindstick.com/qa/30686/what-is-a-base-class-and-derived-class) should honor the behavior of its [base class](https://www.mindstick.com/interview/2426/what-is-the-base-class-for-error-and-exception).

### Bad Example:

```cs
public class Bird { public virtual void Fly() { } }

public class Ostrich : Bird
{
    public override void Fly() => throw new NotImplementedException();
}
```

> Ostrich can’t fly, but the base class assumes all birds can — violates LSP.

### Good Example:

```cs
public abstract class Bird { }
public interface IFlyingBird { void Fly(); }

public class Sparrow : Bird, IFlyingBird { public void Fly() { } }
public class Ostrich : Bird { }
```

## I — [Interface Segregation Principle (ISP)](https://www.mindstick.com/interview/34041/understanding-solid-principles-in-object-oriented-design)

**Clients should not be forced to depend on [interfaces](https://www.mindstick.com/forum/1033/issue-with-generics-interfaces-and-casting) they do not use.**

> Prefer many **small interfaces** over one large one.

### Bad Example:

```cs
public interface IWorker
{
    void Work();
    void Eat();
}

public class Robot : IWorker
{
    public void Work() { }
    public void Eat() => throw new NotImplementedException(); // ISP violation
}
```

### Good Example:

```cs
public interface IWorkable { void Work(); }
public interface IFeedable { void Eat(); }

public class Human : IWorkable, IFeedable { public void Work() { } public void Eat() { } }
public class Robot : IWorkable { public void Work() { } }
```

## D — Dependency Inversion Principle (DIP)

**High-level modules should not depend on low-level modules. Both should depend on abstractions.**

> Rely on **interfaces**, not concrete classes.

### Bad Example:

```cs
public class FileLogger
{
    public void Log(string message) { }
}

public class AuthService
{
    private FileLogger logger = new FileLogger(); // tightly coupled
}
```

### Good Example:

```cs
public interface ILogger
{
    void Log(string message);
}

public class FileLogger : ILogger
{
    public void Log(string message) { }
}

public class AuthService
{
    private readonly ILogger _logger;
    public AuthService(ILogger logger) => _logger = logger;
}
```

> Now you can inject any logger (e.g., `DbLogger`, `ConsoleLogger`) without modifying `AuthService`.

## Summary Table

| Principle | Goal | .NET Example |
| --- | --- | --- |
| **SRP** | One class = one job | Separate `EmailService`, `InvoiceService` |
| **OCP** | Extend without modify | `IReportGenerator` interface |
| **LSP** | Substitutable behavior | Avoid base classes with unsupported methods |
| **ISP** | Small, focused interfaces | `IWorkable`, `IFeedable` |
| **DIP** | Depend on abstractions | Use `ILogger`, inject via constructor |

Let me know if you'd like:

- A full SOLID-based project structure
- Real-world Web API or [ASP.NET MVC](https://www.mindstick.com/forum/12890/how-can-asp-dot-net-mvc-controller-return-an-image) examples using these principles

---

Original Source: https://www.mindstick.com/articles/339591/explain-solid-principles-with-real-life-dot-net-examples

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
