How to create .NET interface with static members?
While I agree with other posters that singletons are very much over used, a possible solution to your question is to provide an abstract base class with a type parameter of the derived singleton:
public abstract class Singleton<T> where T : Singleton<T>
{
private static T _instance;
public static T Instance
{
get { return _instance; }
protected set { _instance = value; }
}
}
Any class that derives from Singleton will have a static Instance property of the correct type:
public class MySingleton : Singleton<MySingleton>
{
static MySingleton()
{
Instance = new MySingleton();
}
private MySingleton() { }
}
Before using something like this though you should really think about whether a singleton is required or if you are better off with a normal static class.
An Interface can't, to my knowledge, be a Singleton since it doesn't actually exist. An Interface is a Contract that an implementation must follow. As such, the implementation can be a singleton, but the Interface can not.