Latest

random

Difference between Static Class and Singleton Pattern in C#

Singleton Pattern vs Static Class C#

In C#, many of us have used static and singleton pattern in our applications. But sometimes a question rises in mind that "Why we use singleton design pattern if a dotnet Static Class serves the purpose?" Somewhere you might have asked "When to use singleton pattern or Static class?"

In this article we will learn about static class in dotnet, singleton object and what is difference between Singleton Pattern and Static class in brief.
difference between static class and singleton pattern in c#, static class dotnet, singleton class in C# example, singleton object, real time example of singleton class, when to use singleton pattern, why we use singleton design pattern
real time example of singleton class

Static Class

  • A class having all static methods.
  • You cannot create the instance of static class.
  • Its loaded automatically by the .Net framework CLR when the program or namespace containing the class is loaded.
  • Static class cannot have constructor.
  • We cannot pass the static class to method.
  • We cannot inherit Static class to another Static class in C#.
  • Better performance (static methods are bonded on compile time)
  • Static object stores in Stack.
  • We can not clone the static class object.
  • Static class doesn't follow the OOP (Object Oriented Principles)
  • We cannot implement interface with Singleton.

    Singleton Pattern

    • You can create only one instance of the object and reuse it.
    • Singleton instance is created for the first time when the user requested.
    • Singleton class can have constructor.
    • You can create the object of singleton class and pass it to method.
    • Singleton class does not say any restriction of Inheritance.
    • We can dispose the objects of a singleton class but not of static class.Singleton object stores in Heap.
    • We can clone the object of Singleton.
    • Singleton pattern follow the OOP (Object Oriented Principles)
    • We can implement interface with Singleton.
    • Methods can be overridden.
    • Easier to mock then static methods
    • Better at maintaining state.
    • Can be lazy loaded when need (static classes are always loaded).

    I hope you will keep the interview tip while preparing for interview. I would like to have feedback from my blog readers. Your valuable feedback, question, or comments about this article are always welcome. 

    Also If you like this article, don't forget to share this article with your friends and colleagues

    No comments:

    All Rights Reserved by Etechpulse © 2015 - 2016
    Powered By Blogger

    Contact Form

    Name

    Email *

    Message *

    Powered by Blogger.