The Singleton design pattern
The Singleton design pattern allows you to create and reuse a single instance of a class. We could use a static class to achieve almost the same goal, but not everything is doable using static classes. For example, a static class can’t implement an interface. We can’t pass an instance of a static class as an argument because there is no instance. We can only use static classes directly, which leads to tight coupling every time.
The Singleton pattern in C# is an anti-pattern. As a rule of thumb, we should never use it and use dependency injection instead. .NET and C# offer the tools to implement similar functionalities without the drawbacks, hence the recommendation to not use this pattern. One such drawback is poor testability. That said, it is a classic design pattern worth learning to at least avoid implementing it. We explore a better alternative in the next chapter.
Here are a few reasons why we are covering this pattern:
...