Last active
November 10, 2022 16:09
-
-
Save elusive/187380c7d6a0ffae313ce3017a70a873 to your computer and use it in GitHub Desktop.
Singleton Helper
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
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