A Generic Singleton Class

by Larry Spencer Wednesday, July 13, 2011 4:33 PM

Much has been written about how to implement the Singleton pattern in C#. The basic idea, ignoring the aspects of thread-safety:

  • Make the default constructor private so the class cannot be instantiated directly.
  • Make a static member variable that holds the one instance of the class itself.
  • Make an Instance property that returns the static member.

Version 4.0 of the .NET framework introduces the Lazy<T> class, which makes correct implementation easier and better than ever. But you still have to remember how to do it, so let's make it even easier and even better by encapsulating everything in a generic class.

The Generic Singleton Class

using System;
using System.Linq;
using System.Reflection;

namespace SingletonDemo
{
  public abstract class Singleton
  {
    private static readonly Lazy<T> _instance
      = new Lazy<T>(() =>
        {
          var ctors = typeof(T).GetConstructors(
              BindingFlags.Instance
              |BindingFlags.NonPublic
              |BindingFlags.Public);
          if (ctors.Count() != 1)
              throw new InvalidOperationException(String.Format("Type {0} must have exactly one constructor.",typeof(T)));
          var ctor = ctors.SingleOrDefault(c => c.GetParameters().Count() == 0 && c.IsPrivate);
          if (ctor == null)
              throw new InvalidOperationException(String.Format("The constructor for {0} must be private and take no parameters.",typeof(T)));
          return (T)ctor.Invoke(null);
        });

    public static T Instance
    {
      get { return _instance.Value; }
    }
  }
}

Proceeding from top to bottom, we note the following:

  • The class is abstract so it cannot be created directly, but must be derived from.
  • The _instance member is a Lazy<T>, where T is the class for which we want a singleton. 
  • The Lazy class' constructor takes one parameter, which is the Func that produces the singleton.
  • In the well-known pattern, a Singleton class' constructor should be private. This forces consumers of the class to use the Instance property, which gets the one and only instance. Accordingly, our Func looks for a private constructor that takes no parameters.
  • Once our Func has ensured that all is well, it invokes the private, parameterless constructor. Don't worry about the time it takes to do an Invoke; it will only happen once!
  • The Instance property returns the one and only instance of the T class, according to the usual pattern.

A Concrete Instance

All you must do to use the generic Singleton class is

  • Inherit from Singleton<T> as shown below.
  • Provide a private constructor.
using System.Threading;
namespace SingletonDemo
{
    class MySingleton : Singleton<MySingleton>
    {
        int _counter;

        public int Counter 
        { 
            get { return _counter; } 
        }

        private MySingleton()
        {
            _counter = 0;
        }

        public void IncrementCounter()
        {
            // ++_counter;   // BAD!!!
            Interlocked.Increment(ref _counter);
        }
    }
}

Although the Lazy<T> class ensures that the singleton is created in a thread-safe manner, that does not mean that the singleton itself is thread-safe! To underscore this fact, the listing of MySingleton shows how to increment the backing variable for its Counter property.

Using the Singleton Instance

You can now access the single instance of MySingleton with its Instance property. Here is an example. We execute a couple of nested loops, the outer one in parallel, to increment the counter in the singleton a total of 100,000 times. The program prints

Counter=100000.

(By the way, if you change MySingleton.cs to use the BAD method of updating the counter, the final result will usually print as something less than 100000.)

using System;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace SingletonDemo
{
  class Program
  {
    static void Main(string[] args)
    {
      Parallel.For(0, 100, i =>
        {
          for (int j = 0; j < 1000; ++j)
              MySingleton.Instance.IncrementCounter();
        });

      Console.WriteLine("Counter={0}.", MySingleton.Instance.Counter);
      Console.ReadLine();
    }
  }
}

Tags:

All | General

About the Author

Larry Spencer

Larry Spencer develops software with the Microsoft .NET Framework for ScerIS, a document-management company in Sudbury, MA.