/* * This is the static method that controls the access to the singleton instance. * On the first run, it creates a singleton object and places it into the static field. * On subsequent runs, it returns the client existing object stored in the static field. */ public static NormalSingleton GetInstance() { if (_instance == null) { _instance = new NormalSingleton(); } return(_instance); }
public static void TestSingleton() { // The client code. NormalSingleton s1 = NormalSingleton.GetInstance(); NormalSingleton s2 = NormalSingleton.GetInstance(); if (s1 == s2) { Console.WriteLine("Singleton works, both variables contain the same instance."); } else { Console.WriteLine("Singleton failed, variables contain different instances."); } }