Skip to content

Instantly share code, notes, and snippets.

@elusive
Last active November 10, 2022 16:09
Show Gist options
  • Save elusive/187380c7d6a0ffae313ce3017a70a873 to your computer and use it in GitHub Desktop.
Save elusive/187380c7d6a0ffae313ce3017a70a873 to your computer and use it in GitHub Desktop.
Singleton Helper
namespace Elusive
{
using System;
using System.Collections.Concurrent;
/// <summary>
/// Provides easy management of singleton instances when IoC is not available.
/// </summary>
/// <typeparam name="T"></typeparam>
public static class Singleton<T>
where T : new()
{
private static readonly ConcurrentDictionary<Type, T> _instances = new ConcurrentDictionary<Type, T>();
public static T Instance
{
get
{
return _instances.GetOrAdd(typeof(T), (t) => new T());
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment