TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

C# Private Constructor

This C# example program uses a private constructor. Private constructors are needed by singletons.

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: Modifiers, Syntax and Concepts

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 and System.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 Class

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.


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf