Wednesday, June 15, 2016

C#: Singletons (Static classes) Old School versus Really Old School

I have been asked during interviews how to implement a singleton using C# and how to implement a static class using C#. I started coding in C# during Alpha 3 of .NET two years before .NET shipped in 2002.

Decorating a Class with the static Keyword (the correct way)

The static key work was introduced for constructors, events, fields, methods, operators, properties in C# 1.0 (2002).In C# 2.0 (2005) the static keyword was applied to decorating classes, hence creating a clear way to define a class a "static class" (a singleton).

The correct answer on how to code a static class (a.k.a. a Singleton) is as follows where the class is declared static "public static class ExampleStaticClass" using the C# 2.0 decorator:

public static class ExampleStaticClass
{
  public static void Method01()
  {
  }

  public static void Method02()
  {
  }
}

Notice that the class is decorated by the static keyword and each method is similarly decorated with the static keyword.  The static class decorator requires that events, fields, methods, operators, and properties of the static class must also be decorate with the static keyword.

It is possible to have one and only one static constructor in a static class then again it is only possible to have one static constructor associated with any class.

A class decorated with the static keyword cannot be used as a base class. This is the same behavior a sealed.

.NET Static Classes Circa .NET 2.0

.NET is rife with static classes including:
public static class Math
public static class Directory
public static class Folder
public static class Path

Notice that each of the above classes are decorated with the C# 2.0 static keyword.

Creating a Static Class Old School (C# 1.0 style circa 2002)

Visual Studio (circa 2002) and Visual Studio 2003 (obviously circa 2003) shipped with C# 1.0 and C# 1.2 respectively. The static keyword existed in these version of C# but it could not be applied to class. The mechanism used to create static class was as follows:

1) Declare the class as sealed which means the class cannot be used as a base class.
2) Declare each event, field, method, operator, and property within the class as static.

There is no requirement that each event, field, method, operator, and property be static. This is a voluntary pattern that developers must follow in order to create a static class in the era of C# 1.0 and 1.2.

.NET Static Classes Circa .NET 1.0/1.2

.NET 1.0/1.2 is also rife with static classes including:
public sealed class Math
public sealed class Directory
public sealed class Folder
public sealed class Path

Notice that each of the above classes are decorated with the sealed keyword which does not necessarily mean static but is used as part of the static/singleton design patter as implemented in C# 1.0/1.2.


2 comments :

  1. Hi Jan, hope your doing great. Are static classes used to implement singleton thread safe? Or locks are still need to be in place to ensure this.

    ReplyDelete
    Replies
    1. Not thread safe at all buddy. The thing is with things like Path, File, Directory and Math no locks are needed. These are well designed classes with no data shared between static methods except the odd constant like e or pi.

      Delete