Why do I need a private constructor?

If a class has a private constructor, it cannot be created. Therefore, if I do not want my class to be created and still use it, I can make it static.

What is the use of a private constructor?

It is also used in the Singleton class, but is there any other use besides this?

(Note. The reason I exclude the singleton case described above is because I donโ€™t understand why we need a singleton at all when a static class is available. You may not answer this question because of my confusion in the question.)

+57
Apr 6 2018-10-06T00:
source share
10 answers

Factory

Private constructors can be useful when using the factory pattern (in other words, the static function used to get an instance of a class, not an explicit instance).

public class MyClass { private static Dictionary<object, MyClass> cache = new Dictionary<object, MyClass>(); private MyClass() { } public static MyClass GetInstance(object data) { MyClass output; if(!cache.TryGetValue(data, out output)) cache.Add(data, output = new MyClass()); return output; } } 

Pseudo-sealed with nested children

Any nested classes that inherit from an outer class can call a private constructor.

For example, you can use this to create an abstract class that you can inherit, but no one else (the internal constructor would also work here to limit inheritance to one assembly, but the constructor of the private constructor should all implementations be nested classes.)

 public abstract class BaseClass { private BaseClass() { } public class SubClass1 : BaseClass { public SubClass1() : base() { } } public class SubClass2 : BaseClass { public SubClass2() : base() { } } } 

Base constructor

They can also be used to create โ€œbasicโ€ constructors called from different, more accessible constructors.

 public class MyClass { private MyClass(object data1, string data2) { } public MyClass(object data1) : this(data1, null) { } public MyClass(string data2) : this(null, data2) { } public MyClass() : this(null, null) { } } 
+59
Apr 6 2018-10-06T00:
source share
โ€” -

As pointed out by Stefan, Adam, and others, private constructors are useful when it is undesirable for a class to be created by code outside the class . Singletones, factories, objects of static methods are examples of where the possibility of restricting a type constructor is useful for enforcing a specific pattern.

To answer the second part of your question about why singletones are needed if static classes exist: single and static classes are not equivalent.

For example, a singleton class can implement an interface; a static class cannot. A singleton object can be passed to methods as a parameter - this is not so easy to do with static classes without resorting to shell objects or reflection. There are also cases where you can create an inheritance hierarchy in which one (or more) of the leaf classes is single-point โ€” this is also not possible for static classes. As another example, you may have several different singletones, and you can create an instance of one of them at runtime based on environmental or configuration parameters - this is also not possible for static classes.

It is important to understand the features of the language and choose the right one for the job - they are for some reason.

+30
Apr 6 2018-10-06T00:
source share

Sometimes you cannot create an instance of a class. This makes it explicit and provides it at the compiler level.

Singleton is just one case. Classes of constants, classes of static methods, and other types of patterns determine that a class should not be realistic.

+15
Apr 6 2018-10-06T00:
source share

Well, if your only goal is that you do not want it to be created, then making it static is enough.

If, otoh, you just don't want frm to implement it outside the class (maybe you want users to only get it using a static factory in the class) - then you need a private ctor to allow these public static plants to instantiate.

Historically, remember that creating a static class was not always around ... Creating private private was a way to make it non-instantiable (is that a word?) Before a static keyword can be applied to a class ...

+2
Apr 6 2018-10-06T00:
source share

You can use it to force a singleton instance or create a factory .

The static method can call a private constructor to create a new instance of this class.

For example, a singleton instance:

 public class Foo { private Foo (){} private Foo FooInstance {get;set;} public static Foo GetFooInstance () { if(FooInstance == null){ FooInstance = new Foo(); } return FooInstance; } } 

This allows you to create only one instance of the class.

+2
Apr 6 '10 at 15:00
source share

Purpose of creating a private constructor in the class

  • To restrict the inherited class.

  • Constrain an instance of a class or create multiple instances / objects.

  • To achieve a singleton design pattern.

     public class TestPrivateConstructor { private TestPrivateConstructor() { } public static int sum(int a , int b) { return a + b; } } class Program { static void Main(string[] args) { // calling the private constructor using class name directly int result = TestPrivateConstructor.sum(10, 15); // TestPrivateConstructor objClass = new TestPrivateConstructor(); // Will throw the error. We cann't create object of this class } } 
+2
Feb 03 '17 at 4:40
source share

As for the singleton, singleton is a design pattern used when the environment and requirements satisfy similar motives for using the pattern; static classes are a language function.

As the Answer to LBushkin is discussed, while some of the goals of using singleton can be accomplished using static classes, a particular implementation of singleton can exceed the set of functions of static classes alone.

+1
Apr 6 2018-10-06T00:
source share

If the ONLY class has private constructors, it cannot be created externally.

You can also have private constructors and public constructors with different signatures.

0
Apr 6 2018-10-06T00:
source share

If you want to create a factory for a class, you can use a private constructor and add static "factory" methods to the class to create the class.

An example of this is the Graphics class with From * methods.

0
Apr 6 2018-10-06T00:
source share
  • One use of a private construct is when we have only a static member.
  • As soon as we provide a constructor that is either private, public or any, the compiler will not allow us to add an open constructor without parameters to the class.
  • If we want to create a class object, even if we have private constructors, then we need to have an open constructor along with a private constructor
0
Mar 27 '17 at 9:09 on
source share



All Articles