Skip to content

Instantly share code, notes, and snippets.

@inceax
Created November 26, 2015 13:29
Show Gist options
  • Save inceax/25c5f5090ae98e375853 to your computer and use it in GitHub Desktop.
Save inceax/25c5f5090ae98e375853 to your computer and use it in GitHub Desktop.
Simple Singleton Template for C#
// Singleton
//
// declaration:
// public class A
// {
// public static A Instance { get { return Singleton<A>.Instance; } }
// public void foo(){}
// }
//
// usage:
// A.Instance.foo();
public class Singleton<T> where T : new()
{
static T instance;
public static T Instance
{
get
{
if (instance == null)
instance = new T();
return instance;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment