See the MSDN pattern here for a qualitative explanation of the singleton pattern.
MSDN recommends you write it below to be thread safe:
using System; public sealed class Singleton { private static volatile Singleton instance; private static object syncRoot = new Object(); private Singleton() {} public static Singleton Instance { get { if (instance == null) { lock (syncRoot) { if (instance == null) instance = new Singleton(); } } return instance; } } }
By the way, this template has the following advantages over the static constructor:
Creating an instance is not performed until the object requests an instance; this approach is called a lazy instance. Lazy instance allows you to avoid creating unnecessary singles when you run the application.
Make sure that it meets your needs, and if so, implement this solution.
saamorim
source share