Featured Article

Solid Principles in C# with Examples: Dot Net Tutorials for Beginners

Kenneth Jul 13, 2026

Welcome to our comprehensive guide on Solid Principles in C# with examples and dot net tutorials. These principles, also known as SOLID, are fundamental to writing maintainable, efficient, and scalable code in C#. They help you understand how to design robust and reusable software by following five basic principles.

solid principles in c# with examples dot net tutorials
solid principles in c# with examples dot net tutorials

But first, let's briefly understand why these principles are important. SOLID principles provide a set of guidelines for creating extensible and flexible solutions. They help you manage complexity by breaking down complex codebases into simpler, more manageable parts. Now, let's dive into each principle with examples and dot net tutorials.

different types of lines are shown in this diagram, with the names and colors on them
different types of lines are shown in this diagram, with the names and colors on them

Single Responsibility Principle (SRP)

The Single Responsibility Principle (SRP) states that a class should have only one reason to change. This means that a class should only have one job or responsibility, making it easier to understand and maintain. Let's consider a simple example:

Nets of a Solids | Geometry |Nets of a Cube |Nets of a Cone & Cylinder
Nets of a Solids | Geometry |Nets of a Cube |Nets of a Cone & Cylinder

Before SRP: ```csharp public class EmailLogger { private readonly List _loggingHistory; public EmailLogger() { _loggingHistory = new List(); } public void Log(string message) { _loggingHistory.Add(message); } public void SendAllEmails() { foreach (var message in _loggingHistory) { SendEmail(message); } } private void SendEmail(string message) { // Logic to send email } } ``` Here, the `EmailLogger` class has two responsibilities: logging and sending emails. Let's refactor it according to SRP: After SRP: ```csharp public class Logger { private readonly List _loggingHistory; public Logger() { _loggingHistory = new List(); } public void Log(string message) { _loggingHistory.Add(message); } } public class EmailSender { public void SendEmail(string message) { // Logic to send email } } ```

Dependency Injection Example

a piece of paper with some diagrams on it
a piece of paper with some diagrams on it

Now, suppose we want to change the logger to write to a file instead of memory. With the SRP implementation, we only need to change the `Logger` class. In the original class, we would need to change both the `EmailLogger` and the `EmailSender` classes, violating the SRP. This is where dependency injection comes into play and is often used to implement SRP.

Open-Closed Principle (OCP)

two different types of dots that are in the same pattern, one is black and white
two different types of dots that are in the same pattern, one is black and white

The Open-Closed Principle (OCP) states that a class should be open for extension but closed for modification. This means that you should design your code in such a way that you can extend its behavior without modifying it. Let's look at an example of a calculator:

Before OCP: ```csharp public class Calculator { public int Add(int a, int b) { return a + b; } public int Subtract(int a, int b) { return a - b; } // Other operations... } ``` Here, we have a `Calculator` class with multiple operations. If we want to add a new operation, we have to modify the `Calculator` class. Let's refactor it according to OCP: After OCP: ```csharp public interface IOperation { int Execute(int a, int b); } public class Addition : IOperation { public int Execute(int a, int b) { return a + b; } } public class Subtraction : IOperation { public int Execute(int a, int b) { return a - b; } } ``` Now, we can add a new operation without modifying the `Calculator` class.

Extension Methods Example

OCP also applies to extension methods. Using extension methods, you can add new functionality to an existing class without modifying it. For example, you can add a new method to the `string` class to convert it to lowercase:

the coordinate diagram for corionate geometrics
the coordinate diagram for corionate geometrics

SOLID principles provide a strong foundation for designing and developing robust, scalable, and maintainable software. By understanding and applying these principles, you can write cleaner and more efficient code. Keep practicing and exploring dot net tutorials to master these principles.

Plant Drawing
Plant Drawing
6.G.4 Nuthing But Nets
6.G.4 Nuthing But Nets
Nets for 3-Dimensional Shapes (video lessons, diagrams, examples, step-by-step solutions)
Nets for 3-Dimensional Shapes (video lessons, diagrams, examples, step-by-step solutions)
the different shapes and sizes of balls are shown in this chart, as well as their names
the different shapes and sizes of balls are shown in this chart, as well as their names
Basics of design elements
Basics of design elements
the types of lines on lined paper with different colors and shapes in each section, including one
the types of lines on lined paper with different colors and shapes in each section, including one
the diagram shows how to draw an inscribed circle
the diagram shows how to draw an inscribed circle
📘 Day 22 – Nyquist Stability Criterion & Solved Examples
📘 Day 22 – Nyquist Stability Criterion & Solved Examples
four different types of circles and dots with the words contrast, alignment, and repection
four different types of circles and dots with the words contrast, alignment, and repection

The journey to becoming a proficient C# developer never stops. Keep exploring new concepts, reading, and writing code. Happy coding!