
- C# Basic Tutorial
- C# - Home
- C# - Overview
- C# - Environment
- C# - Program Structure
- C# - Basic Syntax
- C# - Data Types
- C# - Type Conversion
- C# - Variables
- C# - Constants
- C# - Operators
- C# - Decision Making
- C# - Loops
- C# - Encapsulation
- C# - Methods
- C# - Nullables
- C# - Arrays
- C# - Strings
- C# - Structure
- C# - Enums
- C# - Classes
- C# - Inheritance
- C# - Polymorphism
- C# - Operator Overloading
- C# - Interfaces
- C# - Namespaces
- C# - Preprocessor Directives
- C# - Regular Expressions
- C# - Exception Handling
- C# - File I/O
- C# Advanced Tutorial
- C# - Attributes
- C# - Reflection
- C# - Properties
- C# - Indexers
- C# - Delegates
- C# - Events
- C# - Collections
- C# - Generics
- C# - Anonymous Methods
- C# - Unsafe Codes
- C# - Multithreading
- C# Useful Resources
- C# - Questions and Answers
- C# - Quick Guide
- C# - Useful Resources
- C# - Discussion
How to implement Open Closed principle using C#?
Software entities like classes, modules and functions should be open for extension but closed for modifications.
Definition − The Open Close Principle states that the design and writing of the code should be done in a way that new functionality should be added with minimum changes in the existing code. The design should be done in a way to allow the adding of new functionality as new classes, keeping as much as possible existing code unchanged.
Example
Code Before Open Closed Principle
using System; using System.Net.Mail; namespace SolidPrinciples.Open.Closed.Principle.Before{ public class Rectangle{ public int Width { get; set; } public int Height { get; set; } } public class CombinedAreaCalculator{ public double Area (object[] shapes){ double area = 0; foreach (var shape in shapes){ if(shape is Rectangle){ Rectangle rectangle = (Rectangle)shape; area += rectangle.Width * rectangle.Height; } } return area; } } public class Circle{ public double Radius { get; set; } } public class CombinedAreaCalculatorChange{ public double Area(object[] shapes){ double area = 0; foreach (var shape in shapes){ if (shape is Rectangle){ Rectangle rectangle = (Rectangle)shape; area += rectangle.Width * rectangle.Height; } if (shape is Circle){ Circle circle = (Circle)shape; area += (circle.Radius * circle.Radius) * Math.PI; } } return area; } } }
Code After OpenClosed 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; } } }
- Related Articles
- How to implement Single Responsibility Principle using C#?
- In open circuit key is closed or open ?
- Difference between Open Circuit and Closed Circuit
- What is a closed and open circuit ?
- Difference Between Open and Closed Circulatory Systems
- What is Liskov Substitution principle and how to implement in C#?
- What is dependency inversion principle and how to implement in C#?
- What is Interface segregation principle and how to implement it in C#?
- How to quickly check if a file (workbook) is open or closed in Excel
- Difference between Open Loop and Closed Loop Control System
- Draw rough diagrams to illustrate the following:(a) Open curve (b) Closed curve
- How to open hidden file using C#?
- How to open External Programs using Tkinter?
- How to open a webcam using JavaScript?
- Closed-ended vs. Open-ended Mutual Funds: What is the difference?

Advertisements