Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Private vs Static constructors in .Net

I searched for this a lot, but none of the answers are clear (at-least for me!). Now I'm putting this question in SO, as I believe I can't get a more clarified answer anywhere else.

When should I use a private/static constructor in my class?

I'm fed up of usual answers, so please help me with some real-time examples and advantages/disadvantages of using these constructors.

like image 707
Sandeep Avatar asked Nov 06 '12 09:11

Sandeep


People also ask

Can constructor be static in c# net?

A static constructor doesn't take access modifiers or have parameters. A class or struct can only have one static constructor. Static constructors cannot be inherited or overloaded. A static constructor cannot be called directly and is only meant to be called by the common language runtime (CLR).

Can we have private constructor in static class in C#?

You can't get an instance of a static class while you can get instances of a class having private constructor via static methods. Private constructor is used, for instance, in the Singleton design pattern (see, for istance "Implementing Singleton in C#"[^]).

Why do we need static constructor in C#?

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

What is private constructor in C#?

A private constructor is a special instance constructor. It is generally used in classes that contain static members only. If a class has one or more private constructors and no public constructors, other classes (except nested classes) cannot create instances of this class.


2 Answers

Static constructors: used for initialising static members.

Private constructors: used when you only want a class to be instantiated from within its own code (typically in a static method). For example:

public class Thing
{
    static int Number;

    static Thing()
    {
        Number = 42; // This will only be called once, no matter how many instances of the class are created
    }

    // This method is the only means for external code to get a new Thing
    public static Thing GetNewThing()
    {
        return new Thing();
    }

    // This constructor can only be called from within the class.
    private Thing()
    {
    }
}
like image 67
Dan Puzey Avatar answered Sep 19 '22 05:09

Dan Puzey


When should I use a private constructor in my class?

When you want a constructor, but don't want to expose it to the world. This could be because you have a factory method that calls the constructor (after validation), or because that constructor is called by ctor-chaining (i.e. public Foo(string) : this() { ...}).

Additionally, note that reflection code is often able to use a private constructor - for example serialization or ORM libraries.

Also, in early C# compilers, when you are writing what would now be a static class - having a private constructor was the only way of making it appear uncreatable.

When should I use a static constructor in my class?

When you need to initialize some static state prior to that state being accessed by instances or static methods.

like image 41
Marc Gravell Avatar answered Sep 18 '22 05:09

Marc Gravell