design-patterns/Singleton/ThreadSafeSingleton.cs

30 lines
655 B
C#
Raw Permalink Normal View History

2023-09-04 10:29:51 +00:00
using System;
using System.Threading;
public sealed class ThreadSafeSingleton
{
private ThreadSafeSingleton() { }
private static readonly object _lock = new object();
private static ThreadSafeSingleton _instance;
public static ThreadSafeSingleton GetInstance(string value)
{
if (_instance == null)
{
lock (_lock)
{
if (_instance == null)
{
_instance = new ThreadSafeSingleton();
_instance.Value = value;
}
}
}
return _instance;
}
public string Value { get; set; }
}