top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

Why can’t we use a static class instead of singleton?

+4 votes
537 views

Why can’t we use a static class instead of singleton?

posted Apr 29, 2014 by Khusboo

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

+3 votes
 
Best answer

Intention of a singleton pattern is to ensure that a single instance of a class is instantiated. Following are the reasons for why not to use static class in place of singleton

  • One of the key advantage of singleton over static class is that it can implement interfaces and extend classes while the static class cannot (it can extend classes, but it does not inherit their instance members). If we consider a static class it can only be a nested static class as top level class cannot be a static class. Static means that it belongs to a class it is in and not to any instance. So it cannot be a top level class.
  • Static class will have all its member as static only unlike Singleton.
  • It can be lazily loaded whereas static will be initialized whenever it is first loaded.
  • Singleton object stores in Heap but, static object stores in stack.
  • We can clone the object of Singleton but, we can not clone the static class object.
  • Singleton can use the Object Oriented feature of polymorphism but static class cannot.
answer Apr 30, 2014 by anonymous
Similar Questions
+1 vote
using System;

namespace CareerRideTest
{    
  class A
  {    
    public A()
    {
      Console.WriteLine("I am in A");
    }
  }

  class B : A
  {
    public B()
    {
      Console.WriteLine("I am in B");
    }
  }

  class C : B
  {
    static C()
    {
      Console.WriteLine("I am in Static C");
    }
    public C()
    {
      Console.WriteLine("I am in C");
    }
  }    

  class MainClass
  {
    static void Main(string[] args)
    {
      C obj = new C();    
      Console.ReadKey();    
    }
  }    
}
...