vote buttons
1
1
beginner
0
intermediate
0
advanced
19-Nov-2014 03:57 UTC
K Bonneau
350

1 Answers

vote buttons
1
A private constructor means that the class cannot be instantiated from outside the class using that particular constructor and if all constructors are private the class cannot be instantiated at all from outside the class. Additionally such a class cannot even be inherited from. The only way to instantiate such a class is if we provide a static method, which will instantiate the class from within itself and return the instance.

Actually there are not many practical uses of a private constructor, and even if some situation demands it, there are other alternatives rather than using a private constructor for most of the cases. Following are some examples:

Common initialization code

If you have a class with multiple construtors, and all construtors share some common initialization code, you can place the common code in a separate private constructor which can then be called by other constructors. This way code is shared and no one can initialize the class with the private constructor.  Of course you can create a separate function instead of a constructor, but having the code in the constructor has one advantage: It can initialize read-only fields which other functions cannot. Below is an example:

public class PrivateCtorTest
{
    readonly bool var1;
    readonly string var2;

    //Contains common initialization code
    private PrivateCtorTest()
    {
        var1 = true;
        var2 = "READONLY";
    }

    public PrivateCtorTest(int param1, int param2) 
        :this() //This is the call to private constructor.
    {
        //some code
    }

    public PrivateCtorTest(string xyz)
        : this() //This is the call to private constructor.
    {
        //some code
    }
}

Singleton Pattern

If all constructors are private, the class cannot be instantiated. But we can provide a static function within the class which can create an instance and return it back. As the static function is inside the class it can instantiate using a private constructor. The method being static can be called before directly without needing an instance. Below is an example of a such a use for singleton pattern, which is used when we want only one instance of the class to be created. (There are other ways to implement singleton pattern)

public sealed class TestSingleton
{
    private static TestSingleton _instance;
    private TestSingleton() 
    {
        //Some Code
    }

    public static TestSingleton GetInstance()
    {
        //Simplified.. Ignoring synchronisation issues.
        if (_instance == null)
            _instance = new TestSingleton();
        return _instance;
    }
}

public class Program
{
    static void Main()
    {
        //Getting the instance
        var instance = TestSingleton.GetInstance();
    }
}
A much simplified version:
public sealed class TestSingleton
{
    private static TestSingleton _instance = new TestSingleton();;
    private TestSingleton() 
    {
        //Some Code
    }

    public static TestSingleton GetInstance()
    {
        return _instance;
    }
}
19-Nov-2014 04:00 UTC
K Bonneau
350