Singleton code excerpt, an interview-question

c#, design-patterns

Solution

Thread-safety - multiple instances could be created if GetInstance() is called from competing threads.

Problem

I had another interview question. I thought this was silly, but maybe there is something I am missing. The question asked which GoF pattern this is (answer: singleton), and, if there are any problems, how I solve them. I don't see any problems. I mentioned this is never freed and I expect that from this pattern. That is all I said. Am I missing something? ``` public class Class1 { private static Class1 oInstance = null; private Class1() { } public static Class1 GetInstance() { if (oInstance == null) { oInstance = new Class1(); } return oInstance ; } } ```

Original source

Related problems