Last active
January 3, 2016 02:29
-
-
Save enab-dev/8395616 to your computer and use it in GitHub Desktop.
Simple Singleton pattern implementation using System.Lazy<T>. The extending class needs to implement a private parameterless constructor to complete the pattern. Generic type parameter passed to Singleton should be the extending class type.
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
using System; | |
public abstract class Singleton<T> where T : class | |
{ | |
private static readonly Lazy<T> _instance = new Lazy<T>(() => CreateInstance()); | |
public static T Instance { get { return _instance.Value; } } | |
private static T CreateInstance() | |
{ | |
return Activator.CreateInstance(typeof(T), true) as T; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment