Click here to Skip to main content
15,886,693 members
Please Sign up or sign in to vote.
5.00/5 (3 votes)
See more:
Can Someone Explain whats best use of Private Constructors in C# or in what scenario should Private Constructors be used?
Posted
Comments
Pratik Bhuva 8-Jan-15 10:07am    
Everyone reading this question...
All three answers here are awesome.
Read all answers in ascending.

Expanding on OG's post...

You can also use them to initialize some standard set of fields before other constructors, for example:

C#
public class Sample
{
    int x, y, z;
    string name;
    bool flag;

    private Sample()
    {
        x = 5;
        y = 10;
        z = 15;
        flag = true;
    }

    public Sample(string Name)
       : this()
    {
        //flag, x, y, and z are initialized here
        name = Name;
    }

    public Sample(string Name, bool flag)
       : this(Name)   //Constructor chaining
    {
        //name, flag, x, y, and z are initialized here, but we need to change flag
        this.flag = flag;
    }
}


That way, you reduce repetition of code and make sure that there is only one place if you have to change the default values of the fields. Yes you can do this right when you define the variables but there are instances when you can't or want more logic to initializing them.

Then you can only use the public constructor to create the object, like:

C#
Sample s1 = new Sample("BlahBlah");
Sample s2 = new Sample("BlahBlooBlee", false);
 
Share this answer
 
v3
Comments
Sergey Alexandrovich Kryukov 12-Oct-13 20:33pm    
5ed.
—SA
Pratik Bhuva 8-Jan-15 10:02am    
Very Well Explained.
A minor addition to Ron and OriginalGriff's excellent answers above. Please vote for their answers, not this one :)

Keep in mind that all C# Class definitions create a ("invisible") default Public Constructor with no parameters.

That default Constructor, when evoked, will set the value of all Class fields to their Type's default values.

In early versions of C#, a Private Class Constructor with no parameters was often used with the Class marked as 'Sealed, and the Class was used as a "host" for a collection of Static methods. This type of Class is referred to as a "Utility Class."

Why was the Class marked 'Sealed: to prevent someone attempting to derive from the Class.

Early versions of C# defined a default Constructor for you, if you did not supply one yourself. So, writing a parameterless Private Constructor was a way to make sure the Class could never be instantiated.

Later versions of C# replaced these awkward constraints with ability to define Static Classes: with a Static Class you no longer had to write a Private Constructor, didn't need to mark them 'Sealed, etc.

Can a Private Constructor have Parameters ? Yes. You might do that if you had some reason to have a level of indirection where there were Private variables (or whatever) in the Class that you never wanted set directly from outside an instance of the Class.

To implement that you would implement some form of Public Constructor, or Public Method that created an instance of the Class and did accept Parameters, and then call the Private Constructor, or, as Ron showed, you could just use the Private Constructor to initialize some Private variables. This is a rare case; you can find an example of this type of Class definition in Jon Skeet's "C# In Depth Third Edition," p. 449, Listing 14.29.
 
Share this answer
 
Comments
Pratik Bhuva 8-Jan-15 10:05am    
5ed :)
A private constructor means that the class can't be instantiated from outside the class: the only way to create an instance of the class is by calling a static method within the class which creates an instance, or returns a reference to an existing instance.

Generally, they are used in singleton design patterns, where the code ensures that only one instance of a class can ever be created.

See http://en.m.wikipedia.org/wiki/Singleton_pattern[^] for more info.
 
Share this answer
 
Comments
Ron Beyer 12-Oct-13 17:08pm    
5'd, also expanded on another use of them below.
Sergey Alexandrovich Kryukov 12-Oct-13 20:33pm    
Sure, a 5.
—SA
Pratik Bhuva 8-Jan-15 9:58am    
Short And Sweet Explanation.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900