C# Interview Question #92
What is the Single Responsibility Principle?
SOLID, DI, Testing & Practical C# Mid-Level Intermediate
Quick Interview Answer
The Single Responsibility Principle states that a class or module should have one focused responsibility or one primary reason to change.
Detailed Explanation
The Single Responsibility Principle states that a class or module should have one focused responsibility or one primary reason to change.
For example, a ProductService should not simultaneously handle product business logic, email delivery, file storage, PDF generation, and database infrastructure details.
Separating responsibilities makes code easier to test, maintain, replace, and reason about.
Code Example
public class ProductService
{
private readonly IProductRepository _repository;
public ProductService(IProductRepository repository)
{
_repository = repository;
}
}
// Email behavior belongs in a separate IEmailService,
// persistence in a repository, etc.