Open/closed principle

When creating classes, we need to ensure that the class prohibits any breaking modifications by needing to change internal code. We say that such a class is closed. If we need to change it somehow, we can do so by extending the class. This extensibility is where we say that the class is open for extensions.

Getting ready

You will create a class that determines the skills of a trooper by looking at the class of trooper. We will show you the way many developers create such a class and the way it can be created using the open/closed principle.

How to do it…

  1. Create a class called StarTrooper:
    public class StarTrooper
    {
        
    }
  2. To this class, add an enumerator called TrooperClass to identify the type of trooper we want to return the skills ...

Get C# Programming Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.