0% found this document useful (0 votes)
2 views

SOLID_Principles_CSharp_Examples

The document outlines the SOLID design principles in C# with examples for each principle. It covers the Single Responsibility Principle, Open/Closed Principle, Liskov Substitution Principle, Interface Segregation Principle, and Dependency Inversion Principle, providing both poor and improved code examples for clarity. Each principle emphasizes best practices for software design to enhance maintainability and flexibility.

Uploaded by

smjunaid072
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

SOLID_Principles_CSharp_Examples

The document outlines the SOLID design principles in C# with examples for each principle. It covers the Single Responsibility Principle, Open/Closed Principle, Liskov Substitution Principle, Interface Segregation Principle, and Dependency Inversion Principle, providing both poor and improved code examples for clarity. Each principle emphasizes best practices for software design to enhance maintainability and flexibility.

Uploaded by

smjunaid072
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

SOLID Principles with Simple C# Examples

SOLID Design Principles - C# Examples

S - Single Responsibility Principle (SRP)


A class should have only one reason to change.

Bad:
class Invoice {
public void CalculateTotal() { }
public void PrintInvoice() { } // Printing logic here
}

Better:
class Invoice {
public void CalculateTotal() { }
}

class InvoicePrinter {
public void Print(Invoice invoice) { }
}

O - Open/Closed Principle (OCP)


Software should be open for extension, but closed for modification.

Bad:
class Discount {
public double GetDiscount(string customerType) {
if (customerType == "Regular") return 0.1;
if (customerType == "Premium") return 0.2;
return 0;
}
}

Better:
interface IDiscount {
double GetDiscount();
}

class RegularDiscount : IDiscount {


public double GetDiscount() => 0.1;
}
SOLID Principles with Simple C# Examples

class PremiumDiscount : IDiscount {


public double GetDiscount() => 0.2;
}

L - Liskov Substitution Principle (LSP)


Subtypes must be substitutable for their base types.

Bad:
class Bird {
public virtual void Fly() { }
}

class Ostrich : Bird {


public override void Fly() => throw new Exception("Can't fly!");
}

Better:
interface IBird { }

interface IFlyingBird : IBird {


void Fly();
}

class Sparrow : IFlyingBird {


public void Fly() { }
}

class Ostrich : IBird {


// Doesn't implement Fly
}

I - Interface Segregation Principle (ISP)


Don-t force classes to implement interfaces they don-t use.

Bad:
interface IWorker {
void Work();
void Eat();
}
SOLID Principles with Simple C# Examples

class Robot : IWorker {


public void Work() { }
public void Eat() { } // Irrelevant for robots
}

Better:
interface IWork {
void Work();
}

interface IEat {
void Eat();
}

class Robot : IWork {


public void Work() { }
}

D - Dependency Inversion Principle (DIP)


Depend on abstractions, not on concrete implementations.

Bad:
class EmailService {
public void SendEmail(string message) { }
}

class Notification {
EmailService _email = new EmailService();
public void Send(string msg) => _email.SendEmail(msg);
}

Better:
interface IMessageService {
void Send(string message);
}

class EmailService : IMessageService {


public void Send(string message) { }
}

class Notification {
SOLID Principles with Simple C# Examples

private readonly IMessageService _service;


public Notification(IMessageService service) {
_service = service;
}
public void Send(string msg) => _service.Send(msg);
}

You might also like