1. What is a Private Constructor?
A private constructor is a constructor declared using the private access modifier.
It prevents other classes from directly creating an object using that constructor.
Syntax
public class Employee
{
private Employee()
{
Console.WriteLine("Private Constructor");
}
}
Employee emp = new Employee(); // Compilation Error
Why error?
Because the constructor is private, it can only be accessed from within the Employee class.
2. What is the Purpose of a Private Constructor?
There are three common purposes:
-
Restrict object creation – Prevent other classes from creating objects directly.
-
Implement Singleton Pattern – Control object creation so only one instance is available.
-
Create utility classes – Prevent unnecessary object creation when only static methods are needed.
3. Simple Example – Private Constructor
public class DatabaseConnection
{
// Private Constructor
private DatabaseConnection()
{
Console.WriteLine("Constructor Called");
}
public static void Connect()
{
Console.WriteLine("Database Connected");
}
}
// Usage
DatabaseConnection.Connect();
// Compilation Error
// DatabaseConnection db = new DatabaseConnection();
Output:
Database Connected
Explanation
-
DatabaseConnection()is private. -
Other classes cannot directly create an instance.
-
Connect()is static, so it can be called using the class name. -
This design is useful when object creation is unnecessary.
4. Real-World Example – Singleton Pattern
Suppose an application needs a single instance of a configuration manager.
public sealed class ConfigurationManager
{
private static readonly ConfigurationManager _instance = new();
// Private Constructor
private ConfigurationManager()
{
Console.WriteLine("Instance Created");
}
public static ConfigurationManager Instance
{
get { return _instance; }
}
public void ShowConfig()
{
Console.WriteLine("Configuration Loaded");
}
}
// Usage
var obj1 = ConfigurationManager.Instance;
var obj2 = ConfigurationManager.Instance;
obj1.ShowConfig();
Console.WriteLine(
ReferenceEquals(obj1, obj2)
);
Output:
Instance Created
Configuration Loaded
True
Execution Flow
Application
|
v
ConfigurationManager.Instance
|
v
Private Constructor
|
v
Create Single Instance
|
v
Return Same Instance
|
+------------------+
| |
v v
obj1 obj2
| |
+--------+---------+
|
v
Same Object
Explanation:
-
The private constructor prevents external object creation.
-
The static
_instancefield stores the single instance. -
Both
obj1andobj2reference the same object. -
ReferenceEquals()returnstrue.
5. Private Constructor vs Public Constructor
|
Feature |
Public Constructor |
Private Constructor |
|---|---|---|
|
Accessibility |
Anywhere |
Within the declaring class |
|
Object creation |
Allowed externally |
Restricted externally |
|
Common purpose |
Normal object initialization |
Controlled object creation |
|
Singleton pattern |
Not typically used |
Commonly used |
|
Can accept parameters |
Yes |
Yes |
6. Important Key Points
-
A private constructor cannot be called directly from another class.
-
It can be called from methods or other constructors within the same class.
-
A class can contain both private and public constructors.
-
A class with only private constructors cannot be directly instantiated from outside.
-
A private constructor alone does not guarantee Singleton behavior.
-
A class with only private constructors cannot be inherited by an external derived class because its base constructor is inaccessible.
Interview Answer:
A private constructor is used to restrict direct object creation from outside a class. Its main purposes are to control instance creation, support the Singleton design pattern, and prevent instantiation of utility classes containing only static members.
ASP.NET Core Note: For managing shared services, ASP.NET Core's built-in Dependency Injection with AddSingleton() is generally preferred over manually implementing the Singleton pattern.