C#:.NET:Constructor

.NET Array Dictionary List String 2D Async DataTable Dates DateTime Enum File For Foreach Format IEnumerable If IndexOf Lambda Parse Path Process Property Regex Replace Row Sort Split Static StringBuilder Substring Switch Tuple Window

Private Constructor. A private constructor cannot be externally called. It is used to ensure higher-quality code bases on complex projects. A private constructor forces the class to provide a controlled and unified access pattern.Private
Example. Inside the private constructor, a field is initialized to a constant number. The private constructor ensures that it is impossible for the class to be instantiated directly from external classes.
It provides more compile-time checking for the validity of your program. This improves the long-term validity of the code base—without impairing performance or clarity. This program uses a singleton design pattern.
C# program that uses private constructor

using System;

public sealed class Test
{
    public static readonly Test Instance = new Test(); // Singleton pattern
    public int A; // Instance field
    private Test() // This is the private constructor
    {
	this.A = 5;
    }
}

class Program
{
    static void Main()
    {
	// We can access an instance of this object that was created.
	// ... The private constructor was used.
	Test test = Test.Instance;
	// These statements show that the class is usable.
	Console.WriteLine(test.A);
	test.A++;
	Console.WriteLine(test.A);
    }
}

Output

5
6
The Test class has a private constructor and two fields. And the Program class introduces the Main entry point. The private constructor initializes the public int field A to be equal to 5 at the start of the program's execution.Class
Restricting access to object creation. A private constructor is similar to a public constructor in that it uses the type name followed by a formal parameter list. The formal parameter list in this private constructor shown is empty.

Note: The private constructor simply makes it impossible for external code to instantiate the class freely without using reflection.

Reflection
This can enforce the singleton design concept. The private constructor is a way of writing code that enforces your intent for the class. This essentially allows the compiler to police the project.Compile-Time Error
Singleton. Let's consider the singleton design pattern. This is an easily-understood, widely-adopted design pattern. In the C# language, you can use a private constructor to ensure that external code never incorrectly creates a singleton instance.Singleton

Note: A private constructor restricts access to the constructor. It ensures the object can only be created by a member in the type.

Therefore: The private constructor is a desirable part of the singleton pattern implementation in the C# language.


Summary. We looked at a private constructor in a singleton design pattern. The private accessibility modifier enables greater control over the project-wide usage of your class, without impairing any other aspects of the class.

And: When using a private constructor, patterns such as the factory pattern or the singleton pattern can return instances.