What are solid principles with examples in C#

What are solid principles with examples in C#

Team Triveni

4–6 minutes

SOLID Principles is a coding standard that all developers should have a clear concept for developing software in a proper way to avoid a bad design. It was promoted by Robert C Martin and is used across the object-oriented design spectrum.

Single Responsibility Principle

A class should have one, and only one, reason to change.
One class should only serve one purpose, this does not imply that each class should have only one method but they should all relate directly to the responsibility of the class. All the methods and properties should all work towards the same goal. When a class serves multiple purposes or responsibility then it should be made into a new class.

interface Modem {
 public void dial(String pno);
 public void hangup();
 public void send(char c);
 public char recv();
}

Above code violates single responsibility principle, now take a look at below code which has followed single responsibility principle.

interface DataChannel {
 public void send(char c);
 public char recv();
}
interface Connection {
 public void dial(String phn);
 public char hangup();
}

Open Closed Principle

Entities should be open for extension, but closed for modification. Software entities (classes, modules, functions, etc.) be extendable without actually changing the contents of the class you’re extending.

Please look at the following code :

// Open-Close Principle - Bad example
class GraphicEditor {
public void  drawShape(Shape s) {
if (s.m_type==1)
 drawRectangle(s);
else if (s.m_type==2)
 drawCircle(s);
}
public void drawCircle(Circle r) {....}
 public void  drawRectangle(Rectangle r) {....}
}
class Shape {
 int m_type;
}
class Rectangle extends Shape {
 Rectangle() {
 super.m_type=1;
}
}
class Circle extends Shape {
 Circle() {
 super.m_type=2;
}
}

Issue with Example,

  • Impossible to add a new Shape without modifying GraphEditor
  • Important to understand GraphEditor to add a new Shape
  • Tight coupling between GraphEditor and Shape
  • Difficult to test a specific Shape without involving GraphEditor

So How we can fix this problem see the following code

class GraphicEditor {
 public void drawShape(Shape s) {
  s.draw();
 }
}
class Shape {
 abstract void draw();
}
class Rectangle extends Shape {
 public void draw() {
  // draw the rectangle
 }
}

Liskov Substitution Principle

The Liskov Substitution principle was introduced by Barbara Liskov in her conference keynote “Data abstraction” in 1987. Barbara Liskov and Jeannette Wing formulated the principle succinctly in a 1994 paper as follows:

Let φ(x) be a property provable about objects x of type T.

Then φ(y) should be true for objects y of type S where S is a subtype of T. In simple words, Subclass/derived class should be substitutable for their base/parent class. Basically, it takes care that while coding using interfaces in our code, we not only have a contract of input that the interface receives but also the output returned by different Classes implementing that interface; they should be of the same type.

public class Product
{
 public string Name { get; set; }
 public string Author { get; set; }
}
public class Book : Product {}
public class Movie : Product {}

If someone had a Product object (which was actually a Movie) and asked for the Author, what should it do (a Movie doesn’t have an Author)?

People using derived classes should not encounter unexpected results when using your derived class.

Interface Segregation Principle

A Client should not be forced to implement an interface that it doesn’t use. This rule means that we should break our interfaces in many smaller ones, so they better satisfy the exact needs of our clients.

Similar to the Single Responsibility Principle, the goal of the Interface Segregation Principle is to minimize the side consequences and repetition by dividing the software into multiple, independent parts.Let’s see an example :

interface Worker {
 void work();
 void eat();
}
ManWorker implements Worker {
 void work() {…};
 void eat() {30 min break;};
}
RobotWorker implements Worker {
 void work() {…};
 void eat() {//Not Appliciable For a RobotWorker};
}

How we can fix it

interface Workable {
 public void work();
}
interface Feedable{
 public void eat();
}

Dependency Inversion Principle

High-level modules should not depend on low-level modules. Both should depend on abstractions.

Abstractions should not depend on details. Details should depend on abstractions.

By applying the Dependency Inversion the modules can be easily changed by other modules just changing the dependency module and High-level module will not be affected by any changes to the Low-level module.Wrong use

public class EmployeeService {
 private EmployeeFinder emFinder //concrete class
 public Employee findEmployee(…) {
  emFinder.findEmployee(…)
 }
}

Correct use

public class EmployeeService {
 private IEmployeeFinder emFinder //depends on an abstraction
 public Employee findEmployee(…) {
  emFinder.findEmployee(…)
 }
}

Advantages

  • Code becomes more Testably
  • Provides a principled way to manage dependency.
  • Code that are flexible, robust, and reusable.

Summary

When the developer builds a software following the bad design, the code can become inflexible and more brittle, small changes in the software can result in bugs. For these reasons, we should follow SOLID Principles.

Share on Social Media

    Get The Expert Advice To Grow Your Business Digitally
    Related Blogs
    Micro Frontends Best Practices: Do’s and Don’ts You Need to Know
    Read More: Micro Frontends Best Practices: Do’s and Don’ts You Need to Know
    Enhancing AI Capabilities with RAG in Node.js:A Step-by-Step Tutorial
    Read More: Enhancing AI Capabilities with RAG in Node.js:A Step-by-Step Tutorial
    Understanding Async and Await in C# Programming : A Simplified Guide to Asynchronous Programming in C#
    Read More: Understanding Async and Await in C# Programming : A Simplified Guide to Asynchronous Programming in C#
    Secure Your React Native App with Keycloak Authentication: Comprehensive Guide
    Read More: Secure Your React Native App with Keycloak Authentication: Comprehensive Guide

    Stay ahead of the curve

    Get the latest insights, tutorials, and industry news delivered straight to your
    inbox. Join 10,000+ developers and tech leaders.

    Get In Touch

    What are solid principles with examples in C#

    Team Triveni

    4–6 minutes

    Why Clean SQL Matters When Working with MS SQL Server

    Modern applications run on data. From dashboards and CRMs to ERPs and SaaS platforms, SQL queries form the backbone of business intelligence and real-time operations.

    CTEs allow developers to structure complex logic into clear, readable, and reusable steps, making SQL code easier to debug, safer to modify, and scalable for long-term enterprise use.

    Get practical guidance on structuring complex SQL queries

    Book a free consultation

    Conclusion

    CTEs are not just a SQL feature they are a design pattern for scalable, enterprise-grade database systems. By using Common Table Expressions with SELECT, INSERT, UPDATE, and DELETE, developers gain better control, improved readability, and safer data operations across complex production environments. Whether you’re building SaaS platforms, ERP systems, or high-volume reporting engines, mastering CTEs gives you a long-term architectural advantage.

    Frequently Asked Questions (FAQs)

    Combining RAG and generative AI leads to enhanced personalization in customer interactions, more efficient data retrieval and analysis, and higher-quality content generation, ultimately improving customer satisfaction and loyalty.

    Industries such as e-commerce, healthcare, finance, and entertainment stand to gain significantly from these technologies, thanks to their focus on customer experience, data-driven decisions, and personalized services.

    Share on Social Media

      Get The Expert Advice To Grow Your Business Digitally
      Related Blogs
      What is JavaScript Spread Operator?
      Read More: What is JavaScript Spread Operator?
      How to Start with Enterprise Mobile and IOT Strategy
      Read More: How to Start with Enterprise Mobile and IOT Strategy
      Streamlining EMS:Neo4j’s Power to solve Supply Chain Challenges
      Read More: Streamlining EMS:Neo4j’s Power to solve Supply Chain Challenges
      Mobile-App-Development-Featured
      Mobile App Development Trends for 2025:How We Help Businesses Stay Ahead
      Read More: Mobile App Development Trends for 2025:How We Help Businesses Stay Ahead

      Stay ahead of the curve

      Get the latest insights, tutorials, and industry news delivered straight to your
      inbox. Join 10,000+ developers and tech leaders.

      Get In Touch