// Collection of code snippets by Arne Vajhøj // (likely posted to comp.os.vms/INFO-VAX, INFO-TPU, MACRO32, eksperten.dk, newz.dk, LinkedIn or other place sometime between 1990 and now) using System; namespace E { public class MySingleton { private static MySingleton _instance = null; private static object mylock = new object(); private MySingleton() { } public static MySingleton Instance { get { lock(mylock) { if(_instance == null) { _instance = new MySingleton(); } } return _instance; } } public void DoInt(int v) { Console.WriteLine("DoInt: " + v); } public void DoDouble(double v) { Console.WriteLine("DoDouble: " + v); } public void DoString(string v) { Console.WriteLine("DoString: " + v); } } public class MyFluentSingleton { private static MyFluentSingleton _instance = null; private static object mylock = new object(); private MyFluentSingleton() { } public static MyFluentSingleton Instance { get { lock(mylock) { if(_instance == null) { _instance = new MyFluentSingleton(); } } return _instance; } } public MyFluentSingleton DoInt(int v) { Console.WriteLine("DoInt: " + v); return this; } public MyFluentSingleton DoDouble(double v) { Console.WriteLine("DoDouble: " + v); return this; } public MyFluentSingleton DoString(string v) { Console.WriteLine("DoString: " + v); return this; } } public class Program { public static void Main(string[] args) { // refer every time MySingleton.Instance.DoInt(123); MySingleton.Instance.DoDouble(123.456); MySingleton.Instance.DoString("ABC"); // save ref MySingleton ms = MySingleton.Instance; ms.DoInt(123); ms.DoDouble(123.456); ms.DoString("ABC"); // fluent MyFluentSingleton.Instance.DoInt(123).DoDouble(123.456).DoString("ABC"); } } }