Private Constructors in C#
Prerequisite: Constructors in C#
Private Constructor is a special instance constructor present in C# language. Basically, private constructors are used in class that contains only static members. The private constructor is always declared by using a private
keyword.
Key Points
- It is the implementation of a singleton class pattern.
- Use private constructor when class have only static members.
- Using private constructor, prevents the creation of the instances of that class.
- If a class contains only private constructor without parameter, then it prevents the automatic generation of default constructor.
- If a class contains only private constructors and does not contain public constructor, then other classes are not allowed to create instances of that class except nested class.
Syntax :
private constructor_name
{ // Code }
Note:
If we don’t use any access modifier to define a constructor, then the compiler takes that constructor as a public.
Example 1:
C#
// C# program to illustrate the // concept of private Constructor using System; public class Beginner { // Private constructor // without parameter private Beginner() { Console.WriteLine( "Private Constructor" ); } } // Driver Class class GFG { // Main Method static void Main() { // This line raise error because // the constructor is inaccessible Beginner obj = new Beginner(); } } |
Compile-time Error:
prog.cs(40, 13): error CS0122: `Beginner.Beginner()’ is inaccessible due to its protection level
Explanation:
In the above example, we have a class named as Beginner. Beginner class contains the private constructor, i.e.
private Beginner()
. In the Main method, when we are trying to access private constructor using this statement
Beginner obj = new Beginner();
, the compiler will give an error because the constructor is inaccessible.
Example 2:
C#
// C# program to illustrate the // concept of private Constructor using System; class Beginner { // Variables public static string name; public static int num; // Creating private Constructor // using private keyword private Beginner() { Console.WriteLine( "Welcome to Private Constructor" ); } // Default Constructor // with parameters public Beginner( string a, int b) { name = a; num = b; } } // Driver Class class GFG { // Main Method static void Main() { // This line raises error because // the constructor is inaccessible // Beginner obj1 = new Beginner(); // Here, the only default // constructor will invoke Beginner obj2 = new Beginner( "Ankita" , 2); // Here, the data members of Beginner // class are directly accessed // because they are static members // and static members are accessed // directly with the class name Console.WriteLine(Beginner.name + ", " + Beginner.num); } } |
Output:
Ankita, 2
Explanation:
The above example contains a class named as Beginner. This Beginner class contains two static variables, i.e. name, and num and two constructors one is a private constructor, i.e. private Beginner()
and another one is default constructor with two parameters, i.e. public Beginner(string a, int b)
. In the Main method, when we try to invoke private constructor using this statement Beginner obj1 = new Beginner();
will give an error because the private constructor does not allow to create instances of Beginner class. The only default constructor will invoke.
Contact Us