Loading
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Aravind GovindarajRank #430 · 5.8K reputationPosted Nov 20, 2022, 9:32 AM EST
The Open Close principle is one that is used to open extend and close for any changes.
For eg.,
RBI has a set of norms and would like to extend the logic in SBI business cases. So we are extending the RBI library and changing in our SBI Library, In this approach we are not modifying anything on Main placeholder however we are molding it in on required place on demand basis.
Pankajkumar PatelRank #181 · 13.4K reputationPosted Nov 21, 2022, 6:57 AM EST
Hi Naresh Beniwal,
Open Closed Principle (OCP) states that a class must be open to be extended but closed to changes. Example in C# is as following:
BankAccount
// Not following the Open Closed Principle public enum AccountType { SavingsAccount = 1, LongTermSavingsAccount = 2 } public class BankAcount { public string AccountNumber { get; set; } public string AccountTitle { get; set; } public AccountType BankAccountType { get; set; } public float CalculateInterest(float accountBalance) { // Add an IF statement for each type of new bank account -- violates OCP if (BankAccountType == AccountType.SavingsAccount) return accountBalance * 0.02F; if (BankAccountType == AccountType.LongTermSavingsAccount) return accountBalance * 0.04F; else return accountBalance; } }BankAccountBase
// Following the Open Closed Principle public interface IBankAccount { float CalculateInterest(float accountBalance); } public abstract class BankAccountBase { public string AccountNumber { get; set; } public string AccountTitle { get; set; } public AccountType BankAccountType { get; set; } }SavingsAccount
public class SavingsAccount : BankAccountBase, IBankAccount { public float CalculateInterest(float accountBalance) { return accountBalance * 0.02F; } }LongTermSavingsAccount
public class LongTermSavingsAccount : BankAccountBase, IBankAccount { public float CalculateInterest(float accountBalance) { return accountBalance * 0.04F; } }How to use
var bankAccount = new BankAcount() { AccountNumber = "100AAA", AccountTitle = "Mr. John Doe", BankAccountType = AccountType.SavingsAccount }; Console.WriteLine($"The Interest amount for {bankAccount.AccountTitle} is ${bankAccount.CalculateInterest(1000)}"); //Calling class following Open Closed Principle var savingsBankAccount = new SavingsAccount() { AccountNumber = "100AAA", AccountTitle = "Mr. John Doe", BankAccountType = AccountType.SavingsAccount }; Console.WriteLine($"The Interest amount for {savingsBankAccount.AccountTitle} is ${savingsBankAccount.CalculateInterest(1000)}");Hope, this will help you!
Vishal YelveRank #178 · 13.6K reputationPosted Nov 21, 2022, 4:52 AM EST
The open-closed principle states that a software component like method, class or module should be open for extension, but closed for modification.
Please check below links for your reference
https://code-maze.com/open-closed-principle/
https://dotnettutorials.net/lesson/open-closed-principle/
https://levelup.gitconnected.com/4-ways-to-achieve-open-closed-principle-in-c-d2d0ebea4b86
NitinRank #140 · 16.1K reputationPosted Nov 29, 2022, 1:42 AM EST
https://www.c-sharpcorner.com/UploadFile/pranayamr/open-close-principle/
Rajanikant HawaldarRank #110 · 20.2K reputationPosted Nov 20, 2022, 4:02 AM EST
https://www.c-sharpcorner.com/article/solid-principles-in-c-sharp-open-closed-principle/
https://www.c-sharpcorner.com/blogs/open-close-principle-with-example1
Brahma Prakash ShuklaRank #633 · 3.8K reputationPosted Nov 29, 2022, 12:49 AM EST
Example of open close principle-
namespace SolidPrinciples.Open.Closed.Principle.After{ public abstract class Shape{ public abstract double Area(); } public class Rectangle: Shape{ public int Width { get; set; } public int Height { get; set; } public override double Area(){ return Width * Height; } } public class Circle : Shape{ public double Radius { get; set; } public override double Area(){ return Radius * Radius * Math.PI; } } public class CombinedAreaCalculator{ public double Area (Shape[] shapes){ double area = 0; foreach (var shape in shapes){ area += shape.Area(); } return area; } } }