Static Classes & Singleton in C#

Posted: August 12, 2012 in .NET, C#
Tags: ,

Singleton is the most widely known Design Pattern. Singleton makes sure that one only one instance of a class is created and the same instance is used by other programs and classes.An implementation of a Singleton class is shown below:

class Program 
    { 
        static void Main(string[] args) 
        { 
            SingletonDemo s = SingletonDemo.GetInstance(); 
            s.M1(); 
            Console.Read(); 
        } 
    } 
    public class SingletonDemo 
    { 
        //Single Instance 
        private static SingletonDemo instance = new SingletonDemo();

        //Private Constructor 
        private SingletonDemo() { }

        //Method to get the single instance 
        public static SingletonDemo GetInstance() 
        { 
            return instance; 
        } 
        public void M1() 
        { 
            Console.WriteLine("SingletonDemo::M1 called"); 
        } 
    }
  

This is quite straightforward. This is just like any normal class, we are just preventing the creation of multiple instances by making the constructor private and the GetInstance method makes sure that the same instance is returned in all the calls to it.

Normally , in an N-Tier application we make our Business Logic & Data Access Layer classes Singletons as in most of the cases they are do not hold any state specific information.

Now the question is Why Can’t We Make Them Static Classes?

Let change this code to make it a static class.

class Program 
{ 
    static void Main(string[] args) 
    { 
        StaticDemo.M1(); 
        Console.Read(); 
    } 
} 
public static class StaticDemo 
{ 
    
    public static void M1() 
    { 
        Console.WriteLine("StaticDemo::M1 called"); 
    } 
} 

This looks all very simple. But there are some restrictions and limitations of the static classes.

  • Static classes cannot be instantiated.Static classes cannot have instance constructors.
    • Static classes can only have static constructors
  • Static classes are sealed. You cannot derive from a static class.
  • Static classes cannot derive from any other classes other than System.Object
  • Static classes cannot implement interfaces.
  • Static classes can only have static members

So from the above list of don’t haves its quite clear that static classes lack certain basic OO features like inheritance & polymorphic behavior.Static classes are containers to logically group a set of members(methods & properties) who are not dependent of any object’s identity. Actually C# is a strict OO language where you can’t put anything outside a class unlike C++. So to group these kind of methods static classes are an option.

So now coming back to the question, should we make our Business Logic/Data Access Layer classes static classes.Yes, if they are not holding any state information and just contains a bunch of methods grouped together, we can. But…

In that case we imposing certain restrictions on the BLL/DAL classes, inability to implement interfaces & derive from base classes in particular.This can be real bottleneck if you are planning to use a IoC container like Spring/Unity to manage your classes or some custom interface based framework. To summarize, I feel should not take away certain basic OO properties from our most important classes and make them static.

Advertisements
Comments
  1. Ibrahim says:

    Clearly Explained.Thank you Sir

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s