Singleton pattern

What’s the singleton pattern? A singleton pattern is an object creation pattern where only one instance of the object can ever be created in the lifetime of the application. In C#, this would be akin to having a static class as that would ensure there is only one instnace of the class for the application’s lifetime. public sealed class MessageService { public static MessageService SingletonMessageService { get; } = new MessageService(); // Auto implemented properties allow us to declare the singleton service in this manner private MessageService() { } } With the above example, there would always be only one instance of MessageService and the only way to use it is by calling it through MessageService....

August 8, 2021 · 3 min · Samuel Tambunan