.NET interview question - when is a singleton not a singleton?
I've read some posts lately with questions people use to ask or have been asked during a job interview, so I wanted to post a tricky question too.
I've talked about singletons in the past, and what we use to think is that a classic singleton class has to be unique into a specific AppDomain. Let's take this simple implementation of the singleton pattern, which is thread safe, too: [edit: made the class sealed to avoid confusion]
public sealed class Singleton
{
private static readonly Singleton instance = new Singleton();
// Private constructor to prevent external instantiation
private Singleton()
{}
public static Singleton Instance
{
get { return instance; }
}
}
There's at least one situation where you can find yourself having multiple instances of the singleton class in the same AppDomain, can you tell when?