Why do we need a private constructor?

C#.Netasp.net

C# Problem Overview


If a class has a private constructor then it can't be instantiated. So, if I don't want my class to be instantiated and still use it, then I can make it static.

What is the use of a private constructor?

Also, it's used in the singleton class, but except for that, is there any other use?

(Note: The reason I am excluding the singleton case above is that I don't understand why we need a singleton at all when there is a static class available. You may not answer this for my confusion in the question. )

C# Solutions


Solution 1 - C#

Factory

Private constructors can be useful when using a factory pattern (in other words, a static function that's used to obtain an instance of the class rather than explicit instantiation).

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 the outer class can access the private constructor.

For instance, you can use this to create an abstract class that you can inherit from, but no one else (an internal constructor would also work here to restrict inheritance to a single assembly, but the private constructor forces all implementations to 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 "base" constructors that are 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) { }
}

Solution 2 - C#

As Stefan, Adam and other have pointed out, private constructors are useful in cases where it is undesirable for a class to be created by code outside of the class. Singletons, factories, static method objects are examples of where being able to restrict constructon of a type is useful to enforce a particular pattern.

To respond to the second part of your question about why singletons are needed if static classes exist: singletons and static classes are not equivalent.

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

It is important to understand the language features and choose the right one for the job - they're there for a reason.

Solution 3 - C#

Sometimes you shouldn't be able to instantiate a class. This makes this explicit and enforces this at the compiler level.

Singletons are just one use case. Constants classes, static methods classes, and other types of patterns dictate that a class should not be instantiable.

Solution 4 - C#

Purpose to create the private constructor within a class

  1. To restrict a class being inherited.

  2. Restrict a class being instantiate or creating multiple instance/object.

  3. To achieve the 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
     	}
     }
    

Solution 5 - C#

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

A static method can call the private constructor to create a new instance of that 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 only one instance of the class to be created.

Solution 6 - C#

Well if your only objective is that you don't want it to be instantiated, then making it static is sufficient.

If, otoh, you simply don't want it to be instaniated frm outside the class, (maybe you only want users to get one by using a static factory on the class) - then you need a private ctor to allow those publicly accessible static factories to instantiate it.

Historically, remember that making a class static has not always been around... Making the ctor private was a way to make it not-instantiatable (is this a word? ) before the static keyword could be applied to a class...

Solution 7 - C#

Regarding singletons - singleton is a design pattern used when the environment and requirements satisfy similar motivations for the pattern's use; static classes are a language feature.

As LBushkin's answer discusses, while some of the goals of using singleton can be met using static classes, a particular implementation of singleton may exceed the feature set of static classes alone.

Solution 8 - C#

If the class ONLY has private constructors, it cannot be instantiated from outside.

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

Solution 9 - C#

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

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

Solution 10 - C#

Private constructors is a special instance constructor. and are used in some cases where we create a class which only have static members, so creating instance of this class is useless, and that is where private constructor comes into play.

If a class has one or more private constructors and no public constructors, other classes (except nested classes) cannot create instances of this class.

example:

class LogClass {

  public static double e = Math.E;  //2.71828

  // Private Constructor:
  private LogClass() { 

 }  
}

The declaration of the empty constructor prevents the automatic generation of a parameter less constructor.

If you do not use an access modifier with the constructor it will still be private by default. Read more: https://qawithexperts.com/tutorial/c-sharp/32/private-constructor-in-c-sharp

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionRasshme ChawlaView Question on Stackoverflow
Solution 1 - C#Adam RobinsonView Answer on Stackoverflow
Solution 2 - C#LBushkinView Answer on Stackoverflow
Solution 3 - C#Stefan KendallView Answer on Stackoverflow
Solution 4 - C#Kush Kant Dayal PuneView Answer on Stackoverflow
Solution 5 - C#kemiller2002View Answer on Stackoverflow
Solution 6 - C#Charles BretanaView Answer on Stackoverflow
Solution 7 - C#Cade RouxView Answer on Stackoverflow
Solution 8 - C#Cade RouxView Answer on Stackoverflow
Solution 9 - C#GvSView Answer on Stackoverflow
Solution 10 - C#Vikas LalwaniView Answer on Stackoverflow