Skip to content

Instantly share code, notes, and snippets.

@enab-dev
Last active January 3, 2016 02:29
Show Gist options
  • Save enab-dev/8395616 to your computer and use it in GitHub Desktop.
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.
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