I'm a c# developer.
I have confused with private constructor and static constructor in singleton pattern.
Here is my sample code in below:
standard singleton pattern and it is thread safe:
public class SingletonTest
{
private static readonly Lazy<RedisCacheManager> CacheManager = new Lazy<RedisCacheManager>(() => new RedisCacheManager());
/// <summary>
/// singleton pattern
/// </summary>
private SingletonTest() { }
public static RedisCacheManager Instance
{
get { return CacheManager.Value; }
}
}
second it changed the private constructor to static constructor:
public class SingletonTest
{
private static readonly Lazy<RedisCacheManager> CacheManager = new Lazy<RedisCacheManager>(() => new RedisCacheManager());
/// <summary>
/// static(single object in our application)
/// </summary>
static SingletonTest() { }
public static RedisCacheManager Instance
{
get { return CacheManager.Value; }
}
}
And my question is the second code still one of the singleton pattern or just it always keep a only one object(RedisCacheManager) in our application? Somebody help me,thanks.