Friday, April 26, 2013

Implementing Singleton using Static Constructor

There is another way to implement Singleton design pattern by using static constructor. Static constructors in C# are specified to execute only when an instance of the class is created or a static member is referenced, and to execute only once per AppDomain.
This is a thread-safe implementation and there are other ways to implement Singleton but this for seems to crater most of my requirement. It is highly unlikely that I needed a static variable in the singleton.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace StaticConstructorExp
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Main First Call");
            Console.WriteLine(SingletonTest.Instance.abc);
            Console.ReadLine();
        }
    }
    class SingletonTest
    {
        public String abc;
        private static readonly SingletonTest _obj = new SingletonTest();
        static SingletonTest()
        {
            _obj.abc = "test test";
            Console.WriteLine("static constructor called");
        }
        private SingletonTest()
        {
        }
        public static SingletonTest Instance
        {
            get { return _obj; }
        }
    }
}

Output

Main First Call                                             
static constructor called                                   
test test                                                   

Reference: 
http://csharpindepth.com/Articles/General/Singleton.aspx

Wednesday, April 24, 2013

Static Constructor

A static constructor is used to initialize any static data, or to perform a particular action that needs performed once only. It is called automatically before the first instance is created or any static members are referenced.

Static constructors have the following properties:
  • A static constructor does not take access modifiers or have parameters.
  • A static constructor is called automatically to initialize the class before the first instance is created or any static members are referenced.
  • A static constructor cannot be called directly.
  • The user has no control on when the static constructor is executed in the program.
  • A typical use of static constructors is when the class is using a log file and the constructor is used to write entries to this file.
  • Static constructors are also useful when creating wrapper classes for un-managed code, when the constructor can call the LoadLibrary method.
you can't overload it; a static constructor is useful for initializing any static fields associated with a type (or any other per-type operations) - useful in particular for reading required configuration data into readonly fields, etc.

Static constructor are not thread safe. If there are multiple threads calling the constructor, then it will only initialize once and break your program.

Example
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace StaticConstructorExp
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Main First Call");
            Test.myMethod();
            Console.ReadLine();
        }
    }
    class Test
    {
        static Test()
        {
            Console.WriteLine("Test Constructor Call");
        }
        public static void myMethod()
        {
            Console.WriteLine("MyMethod Call");
        }
    }
}

Output

Main First Call                                       
Test Constructor Call                                 
MyMethod Call