Private Constructors in C#
Last Updated :
10 Sep, 2023
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 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();
}
}
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:
C#
// 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);
}
}
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.
Similar Reads
C# | Inheritance in Constructors Introduction: Inheritance in constructors is a feature in C# that allows a derived class to inherit the constructor of its base class. This means that the derived class can use the constructor of the base class to initialize its own fields and properties. This feature saves a lot of code duplication
8 min read
C# Constructors Constructor is a special method of the class which gets automatically invoked whenever an instance of the class is created. Constructors in C# are fundamental components of object-oriented programming. Like methods, It contains the collection of instructions that are executed at the time of Object c
5 min read
C# | Default Constructor If you don't provide a constructor for your class, C# creates one by default that instantiates the object and sets member variables to the default values as listed in the Default Values Table. Constructor without any parameters is called a default constructor. In other words, this type of constructo
2 min read
Destructors in C# Destructors in C# are methods inside the class used to destroy instances of that class when they are no longer needed. The Destructor is called implicitly by the .NET Frameworkâs Garbage collector and therefore programmer has no control as when to invoke the destructor. An instance variable or an ob
3 min read
Invoking an overloaded constructor using this keyword in C# Prerequisite : Constructors in C#C# provides a powerful keyword known as this keyword and this keyword has many usages. Here we use this keyword to call an overloaded constructor from another constructor.Important Points: When you use this keyword to call a constructor, the constructor should belong
3 min read
C# Scope of Variables The part of the program where a particular variable is accessible is termed the Scope of that variable. A variable can be defined in a class, method, loop, etc. In C/C++, all identifiers are lexically (or statically) scoped, i.e., the scope of a variable can be determined at compile time and is inde
4 min read