Created
November 26, 2015 13:29
-
-
Save inceax/25c5f5090ae98e375853 to your computer and use it in GitHub Desktop.
Simple Singleton Template for C#
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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