C # Singleton Pattern

I use the singleton pattern in many places, sometimes the constructor does nothing, in other cases it initializes things. I was wondering if there is a way to create an abstract class to slightly reduce code repetition, i.e. I do not need public static readonly Singleton _Instance = new Singleton();in each separate class, only one base class. I understand that interfaces are not an option.

I tried using the following (taken from here );

public abstract class Singleton<T> where T : new()
{
    static Singleton()
    {
    }

    private static readonly T _Instance = new T();

    public static T Instance
    {
        get { return _Instance; }
    }
}

The problem is that I cannot override the constructor for cases when I need to initialize things. Is what I'm trying to do even possible? Or should I continue to do what I am doing and not worry about the base singleton class?

+3
source share
1

, ,

, . , , . , - , , , , .

, ? , , ?

, , . , -. , , , , , , .

, , , , ..

+8

All Articles