Welcome to the second module of our interview preparation course. In the previous lesson, we established a strong foundation by exploring abstract classes and interfaces—the C# tools for defining contracts and common base functionality. You learned how they enable polymorphism and help structure class hierarchies.
Today, we shift from the "what" to the "why" and "how" of good object-oriented design. We will begin our study of the SOLID principles, a set of five design guidelines that are the bedrock of modern, professional software engineering. Mastering these will significantly elevate the quality of your code and your design discussions in a technical interview. Our focus for this lesson is the first of these principles: the Single Responsibility Principle (SRP). Your goal is to be able to explain it clearly and demonstrate its application with a C# code example.
1. Defining the Single Responsibility Principle
The name "Single Responsibility Principle" can be misleading. A common misconception is that it means "a class should only do one thing." This is an oversimplification. The formal definition is more precise and powerful: A class should have only one reason to change.
What constitutes a "reason to change"? It's often tied to a business function or an "actor" (a user or stakeholder) who would request a change. For a quick and clear explanation of this crucial distinction, the following short video is excellent.
SOLID - Single Responsibility principle in 3 minutes
This video from Renaro Santos effectively clarifies the core idea of SRP.
Watch the segment from the beginning to the conclusion on roles. Pay close attention to how the presenter connects methods within a single Sale class to different business roles: Tax, Financial, and Database Administration. Each role represents a different reason for the class to change, thus violating SRP.
As the video highlights, if your application's tax calculation logic, profit reporting rules, and database storage mechanism are all in the same class, that class has three distinct reasons to change. A new tax law, a different profit formula, or a database migration would all force you to modify and re-test the same class, increasing the risk of introducing bugs.
Given your experience designing systems with microservices, you can think of this as applying a similar philosophy of "separation of concerns" at the class level. Just as you wouldn't put user authentication, product inventory, and order processing in a single microservice, SRP advises against bundling unrelated responsibilities into a single class.
2. Spotting an SRP Violation: A C# Example
Let's examine a common scenario where SRP is violated. An Invoice class might seem like a natural place to put all invoice-related logic, but this can quickly lead to problems.
Single Responsibility Principle Examples in C# - Dot Net Tutorials
This article from Dot Net Tutorials provides a clear, practical walkthrough of an SRP violation and its solution.
First, read the section "Example Without using Single Responsibility Principle". In the provided Invoice class code, identify the three distinct responsibilities it is handling.
As you can see, the Invoice class is responsible for:
- Invoice Management: The business logic of adding or deleting an invoice.
- Error Logging: Writing exception details to a text file.
- Email Notification: Sending an email after an invoice is added.
This design is brittle. What if the business decides to log errors to a cloud service like Azure Application Insights instead of a local file? You would have to change the Invoice class. What if the marketing team wants to change the format of the confirmation email? Again, you must modify the Invoice class. This class has too many reasons to change.
3. Refactoring for Single Responsibility
The solution is to decompose the class, extracting each responsibility into its own dedicated class. Each new class will now have only one reason to change.
The following practical video demonstrates the thought process behind this kind of refactoring. While the entire video is a comprehensive guide, watching the first refactoring step will solidify the core technique.
Design Patterns: Single Responsibility Principle Explained Practically in C# (The S in SOLID)
In this video, Tim Corey takes a monolithic C# application and refactors it step-by-step to adhere to SRP. We will focus on the first extraction.
Watch the segment from the first refactoring step, where the presenter identifies the "standard messages" (welcome, end application) as a separate responsibility. He extracts this logic from the main program flow into a new StandardMessages class. This demonstrates the fundamental pattern: identify a responsibility, create a new class for it, and move the related logic there.
Now, let's apply this pattern to our Invoice example. We will create separate classes for logging and for sending email.
Single Responsibility Principle Examples in C# - Dot Net Tutorials
This part of the article shows the "after" picture of the refactoring.
Now, read the section "Example Implementing Single Responsibility Principle". Study how the Logger and MailSender classes are created. Then, examine the modified Invoice class. Finally, review the list of advantages at the end.
After refactoring, the design is much cleaner:
Invoiceclass: Its sole responsibility is to handle invoice-specific business logic.Loggerclass: Its sole responsibility is to handle logging. If the logging target changes, only this class is affected.MailSenderclass: Its sole responsibility is to handle email. If the email template or SMTP server changes, only this class is affected.
The Invoice class now delegates tasks to the other classes. This is a crucial concept. It doesn't do the logging or emailing itself; it uses the objects that are specialists in those tasks.
Here is another concise example of a final, clean design that adheres to SRP.

Notice in this image that the OrderProcessor depends on IEmailService, an interface. This is an excellent practice that builds upon our last lesson and points toward the Dependency Inversion Principle, which we will cover later.
How to Explain SRP in an Interview
When asked about the Single Responsibility Principle, structure your answer clearly:
- Definition: Start with the formal definition. "The Single Responsibility Principle states that a class should have one, and only one, reason to change."
- Elaboration: Explain what this means in practice. "This principle encourages us to design classes that are highly focused on a single part of the application's functionality. When a class has multiple responsibilities, changes related to one can unintentionally break the others."
- Example: Provide a concrete C# example. "A common example is a class that both processes a customer order and logs errors to a file. This class has two reasons to change: a change in the order processing logic, or a change in the logging requirements. SRP suggests splitting this into two classes: an
OrderProcessorfor the business logic, and aLoggerfor handling logging. TheOrderProcessorwould then use an instance of theLogger." - Benefits: Conclude with the advantages. "Following SRP leads to a more modular design. Classes become smaller, easier to understand, and easier to test in isolation. This significantly improves maintainability, as changes are localized and have less risk of causing side effects."
Conclusion
You have now analyzed the first SOLID principle. The Single Responsibility Principle is fundamental to writing clean, maintainable, and robust object-oriented code. By ensuring each class has a single, well-defined purpose, you create systems that are easier to develop, test, and evolve over time.
Key Takeaways:
- SRP is not "do one thing," but "have one reason to change."
- Reasons for change are often tied to different business functions or stakeholders.
- Apply SRP by identifying distinct responsibilities within a class and extracting them into their own separate classes.
- The benefits are immense: improved modularity, testability, and maintainability.
In our next lesson, we will explore the Open/Closed Principle (OCP). Now that our classes have a single responsibility, OCP will teach us how to structure them so that they are open for extension but closed for modification, allowing us to add new features without altering existing, tested code.