4

I just wanted to know the main difference between a static constructor and a private constructor.

I know that Static Constructor is used to initialize the static members of a class. A static constructor cannot access non-static members. It executes before the first instance of a class. We can not determine the time of execution. Static Constructor executes by the CLR not by the object of a class. There are no parameterized static constructors since it is handled by the CLR not by the object. Time of execution might be at the loading of contained assembly.

However, Private Constructor is used to restrict a class to be instantiated and to be inherited. Private Constructor is used whenever a class contains only static members.

Apart from this what is the difference between these two in terms of memory usage and other stuff? Which should be used?

Running Rabbit
  • 2,634
  • 15
  • 48
  • 69
  • 2
    "Private Constructor is used whenever a class contains only static members" - that's plain wrong, you are mixing stuff up. "Private Constructor is used to restrict a class to be instantiated and to be inherited" - what does that even *mean*? –  May 19 '15 at 17:12
  • 3
    Why is a raven like a writing desk? – Sergey Kalinichenko May 19 '15 at 17:14
  • 3
    @dasblinkenlight A writing desk is a rest for pens and a raven is a pest for wrens. – Kapol May 19 '15 at 17:17
  • At Insane Poets Club: More helpful - less witty. – Amy B May 19 '15 at 17:19
  • 2
    May be I misunderstood the concept of static and private constructor. and this was the reason I came here to clear my confusion. However, instead of an answer, i received negativity and wrong comments. Thanks everyone – Running Rabbit May 19 '15 at 17:28
  • https://stackoverflow.com/questions/2585836/why-do-we-need-a-private-constructor - This can be helpful. – Indranil Aug 03 '18 at 04:50

2 Answers2

7

Private constructors are still instance constructors - they are not static. They're no different from public constructors, except they define who can call them, just like the difference between a public and private regular method.

Some possible use cases for a private constructor would be for singletons, a private constructor whose functionality is shared by multiple public constructors, or using static methods to build your class instead of constructors.

public class Bar {
     private Bar() { }
     private static readonly Bar _instance = new Bar();
     public static Bar GetInstance() { return _bar; }
}

public class Baz {
    private string _msg;
    private Baz(string msg) { // Not accessible publicly
        _msg = msg;
    }

    // These two are accessible publicly, and both call
    // the private constructor
    public Baz(int i) : this(i + " is an integer") { }
    public Baz(decimal d) : this(d + " is a decimal") { }
}

public class Foo {
    private Foo() { // Not accessible publicly
    }
    public static Foo CreateFoo() {
        // Do some stuff here that you can't normally do in a constructor.
        return new Foo();
    }
}
Joe Enos
  • 39,478
  • 11
  • 80
  • 136
  • 1
    Thank you Joe for answering. This answer my question. Atleast you cared to give a proper reply rather than criticizing my question. – Running Rabbit May 19 '15 at 17:24
  • 1
    No prob. Prior to static classes (which were in C# 2.0 I think), a common pattern for utility classes was to create an abstract class with only a private constructor, then all of the methods inside would be static. You still see that pattern once in a while - might be part of the confusion. – Joe Enos May 19 '15 at 17:31
1

If you are using private constructors to suppress auto-generated public constructors for classes which contain only static members... consider instead using the static keyword on the class itself to prevent any instantiation:

public static class MyMethods
{
  public static void Go()
  {
///TODO
  }
}
Amy B
  • 108,202
  • 21
  • 135
  • 185
  • 1
    The purpose of a private constructor isn't to prevent any instantiation, it's to prevent any *external* instantiation while allowing *internal* instantiation, which is something `static` doesn't allow. – Servy May 19 '15 at 17:16
  • 1
    Yes, and the purpose of the static keyword at the class level is to prevent any instantiation. We are in agreement it seems. – Amy B May 19 '15 at 17:17
  • @Servy, not all languages have the static keyword and thus Yash's confusion over using private constructors to prevent instances of static classes possibly stems from using the trick in other languages. – David Arno May 19 '15 at 17:20
  • @DavidArno Indeed. It is common in legacy C# code to use private constructors for this as well, since static classes weren't possible until C# 2. – Mike Zboray May 19 '15 at 17:28