The Single Responsibility Principle states that a module should have only one reason to change.save the invoice to a storage var invoiceRepository = new InvoiceRepository() create a new invoice var invoice = new Invoice Suppose you need to create a program to manage invoices: public class InvoiceĬonsole.WriteLine( $"Saved the invoice # ") C# Single Responsibility Principle example It should not be responsible for saving the data to a database or performing any other unrelated tasks.īy separating these responsibilities into separate classes, the application becomes more maintainable and easier to extend. It also makes the application more flexible, as changes to one responsibility do not impact other responsibilities.įor example, a class responsible for representing data should only have the responsibility of holding the data. The responsibility refers to the purpose or concern that a class has in an application.īy having a single responsibility, a class becomes more cohesive and easier to maintain. In C#, a software module can be a class or a method of a class. The Single Responsibility Principle states that a software module should have a single responsibility, i.e., it should have one and only one reason to change. The Single Responsibility Principle (SRP) is one of the SOLID principles in software engineering that states that every module or class should have only one. The Single Responsibility Principle (SRP) is the first principle in the five SOLID principles: ![]() Introduction to the C# single responsibility principle Summary: in this tutorial, you’ll learn how to use the C# Single Responsibility Principle to develop software applications that are more maintainable and scalable.
0 Comments
Leave a Reply. |
Details
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |