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.

Important 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 private.

Example 1:



filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to illustrate the
// concept of private Constructor
using System;
  
public class Geeks {
  
    // Private constructor
    // without parameter
    private Geeks()
    {
        Console.WriteLine("Private Constructor");
    }
}
  
// Driver Class
class GFG {
  
    // Main Method
    static void Main() {
  
        // This line raise error because
        // the constructor is inaccessible
        Geeks obj = new Geeks();
    }
}

chevron_right


Compile-time Error:

prog.cs(40, 13): error CS0122: `Geeks.Geeks()’ is inaccessible due to its protection level

Explanation: In the above example, we have a class named as Geeks. Geeks class contains the private constructor, i.e. private Geeks(). In the Main method, when we are trying to access private constructor using this statement Geeks obj = new Geeks();, the compiler will give an error because the constructor is inaccessible.

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to illustrate the
// concept of private Constructor
using System;
  
class Geeks {
  
    // Variables
    public static string name;
    public static int num;
  
    // Creating private Constructor
    // using private keyword
    private Geeks() {
  
        Console.WriteLine("Welcome to Private Constructor");
    }
  
    // Default Constructor
    // with parameters
    public Geeks(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
        // Geeks obj1 = new Geeks();
  
        // Here, the only default 
        // constructor will invoke
        Geeks obj2 = new Geeks("Ankita", 2);
  
        // Here, the data members of Geeks
        // class are directly accessed
        // because they are static members
        // and static members are accessed 
        // directly with the class name
        Console.WriteLine(Geeks.name + ", " + Geeks.num);
    }
}

chevron_right


Output:

Ankita, 2

Explanation: The above example contains a class named as Geeks. This Geeks class contains two static variables, i.e. name, and num and two constructors one is a private constructor, i.e. private Geeks() and another one is default constructor with two parameters, i.e. public Geeks(string a, int b). In the Main method, when we try to invoke private constructor using this statement Geeks obj1 = new Geeks(); will give an error because the private constructor does not allow to create instances of Geeks class. The only default constructor will invoke.



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.