18 November 2026

SOLID Principles in C# for Beginners: Five Rules with Simple Examples

  Asp.Net CS By Example       18 November 2026

SOLID is a set of five design principles that helps you change software without breaking unrelated parts. These are guides for code that is growing, not rules that every tiny class must follow. Let us use a student notification feature to make the ideas practical.

The five ideas
Single Responsibility → one clear reason to change
Open/Closed → extend behaviour without editing stable code
Liskov Substitution → a replacement must keep its promise
Interface Segregation → ask for only the methods a client needs
Dependency Inversion → depend on an abstraction

1. Single Responsibility Principle

Imagine a StudentService that validates students, writes them to a database, sends email, and generates PDF reports. A change to the email provider could force changes in the student logic. Give each concern its own clear place: a validator, repository, notification service, and report service. A class may have several methods; the question is whether they serve one responsibility.

2. Open/Closed Principle

If your notification code has a growing if chain for email, SMS, and push messages, add a new implementation of a notification interface instead of editing the calling service each time. You still need to change some composition code to register a new implementation. The point is to keep the tested calling behaviour stable.

3. Liskov Substitution Principle

When code accepts an INotifier, any implementation should honour its contract. A replacement that silently discards every message or throws for a supported call breaks callers' expectations. Clear contracts and tests help spot this.

4. Interface Segregation Principle

Do not force a simple email sender to implement unrelated methods such as PrintReport() and ExportCsv(). Use small interfaces like INotifier and IReportExporter, chosen for the clients that need them.

5. Dependency Inversion Principle

A high-level student service should depend on a notification contract, rather than constructing a specific email sender inside its method. Here is a compact C# example:

using System;

INotifier notifier = new ConsoleNotifier();
var service = new StudentWelcomeService(notifier);
service.Welcome("Asha");

public interface INotifier
{
    void Send(string message);
}

public sealed class ConsoleNotifier : INotifier
{
    public void Send(string message) => Console.WriteLine(message);
}

public sealed class StudentWelcomeService
{
    private readonly INotifier _notifier;

    public StudentWelcomeService(INotifier notifier)
    {
        _notifier = notifier;
    }

    public void Welcome(string studentName)
    {
        _notifier.Send($"Welcome, {studentName}!");
    }
}

Output: Welcome, Asha! The service knows what message to send. The notifier decides how to send it. You can later use an email implementation or a test double without rewriting Welcome.

Visual flow
StudentWelcomeService → INotifier contract ← ConsoleNotifier or EmailNotifier

How this connects to ASP.NET Core

ASP.NET Core's dependency injection container can supply an implementation through a constructor. For example, after creating an EmailNotifier implementation, register it with builder.Services.AddScoped<INotifier, EmailNotifier>();. The service then receives the interface through its constructor. Choose a service lifetime that matches how the implementation uses resources.

Practice

  1. Add SmsNotifier that implements INotifier and prints “SMS: ” before the message.
  2. Switch the first line to INotifier notifier = new SmsNotifier();. Did you need to change StudentWelcomeService?
  3. Think of one class in your project that handles two unrelated jobs. How would you split it?

Key point: Interfaces and extra classes have a cost. Introduce them when they make a real change easier to understand and test.

Further reading: Microsoft Learn: Architectural principles.

logoblog

Thanks for reading SOLID Principles in C# for Beginners: Five Rules with Simple Examples

Previous
« Prev Post

No comments:

Post a Comment

Please do not enter any spam link in the comment box.