3
Reply

What is the difference between Static and Private Constructor and when to use them?

Rishi Gupta

Rishi Gupta

14y
14.9k
0
Reply

    Reference: MSDN

    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 unmanaged code, when the constructor can call the LoadLibrary method.

    • If a static constructor throws an exception, the runtime will not invoke it a second time, and the type will remain uninitialized for the lifetime of the application domain in which your program is running.

    Private Constructor:

    A private constructor is a special instance constructor. It is commonly used in classes that contain static members only. If a class has one or more private constructors and no public constructors, then other classes (except nested classes) are not allowed to create instances of this class. For example:

    class NLog 
    {
    // Private Constructor:
    private NLog() {}

    public static double e = 2.71828;
    }

    The declaration of the empty constructor prevents the automatic generation of a default constructor. Note that if you don't use an access modifier with the constructor it will still be private by default. However, the private modifier is usually used explicitly to make it clear that the class cannot be instantiated.

    Private constructors are useful to prevent creation of a class when there are no instance fields or methods, such as the Math class, or when a method is called to obtain an instance of a class.


    Reference:msdn
    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 unmanaged code, when the constructor can call the LoadLibrary method.

    • If a static constructor throws an exception, the runtime will not invoke it a second time, and the type will remain uninitialized for the lifetime of the application domain in which your program is running.


    Hi Rishi,

    These are few points I know about Static and private constructors. Hope it helps. :)

    - Static Contructor is the ideal place to inintialise static members of a class when its value is not known at compile time and may be it needs to be fetched from database or some file.
    Some imp points about static constructors 
               1. They get execute only once, regardless of how many class instances are cerated.
               2. A class/structure can have only one static constructor
               3. Static constructor does not have access modifier and it does not take any parameters.
     
    - Private Constructor is used to control object creation of a class.
    The trick is, you have to first provide a static method which will call the private constructor based on some business condition.
    Some points about private constructor in contrast to static constructor
               1. It can execute as many times as you call it.
               2. A class/structure can have as many as overloaded versions of private constructors.