Singleton in c#

In your recent interviews, You have been asked to implement the singleton pattern in c#. In this article, I will try to explain what the interviewers wanted to hear ...

Understanding the Problem

The first thing the candidate needs to know is what is the singleton pattern.

If the interviewer was not kind enough to explain what is the singleton pattern - you can find about in Wikipedia

In software engineering, the singleton pattern is a design pattern that restricts the instantiation of a class to one object.

Let's see what the requirements from the singleton class are:

  • The class should provide a mechanism to create one and only one object.
  • The class should provide a mechanism to access this single object.

How would you implement those requirements. In the next part we will see the naive solution to this problem