Private Constructors in C#

In this article, we’ll explain about Private Constructors in C# with Examples. This article will be useful to both beginners and professional C# developers. We hope at the end of this article, all your doubts regarding Private Constructors in C# will be cleared.

What is default Constructor?

Before moving towards Private Constructors, let’s check default Constructor first. Every class is having a default constructor or explicitly we can create constructor. By default, constructor is public, but we can make it private as per our needs. How the default constructor appears. Let’s see below   

Private Constructors in C#
Output will be as:
7. default Output - https://codeconfig.in
Private constructor will appear as (By changing access modifier from public to private)
Private Constructors in C#

Important points of Private Constructors

  • Private constructor is a special constructor which is used in a class to assign the value to private readonly variable.
  • The main purpose of creating private constructor is to restrict the class from being instantiated. When you’ll try to create object, you’ll see the error as “Class is inaccessible due to its protection level”.
Private Constructors in C#
  • If a class has one or more private constructor and no public constructor, then other classes are not allowed to create instance of this class; this means you can neither create the object of the class nor it can be inherited by other classes.
Private Constructors in C#

Use of Private Constructor

  • It is used to stop a class to be inherited
  • It is used to stop instantiation of class. (Object cannot be created)
  • It is used in singleton design patterns so that only one instance of a class can be created.
  • Private Constructor is used to assign the value to Private readonly variable like.
Private Constructors in C#

Private Constructor VS Sealed class

We can convert above class in to Sealed class (shown below) by decorating the class with Sealed keyword. By using this we can increase the protection level of our class. This sealed class can be instantiated but not available for inheritance. But class with Private constructor can neither be inherited nor instantiated. Sealed class will appear as:

Private Constructors in C#
NOTE:
Ctor is a snippet and used to create default constructor. Just type Ctor in your C# class and double tab, it will write default constructor for you.

Can we create object of class having private constructor?

No, class with private constructor cannot be instantiated. Its object cannot be created. You will get compile time error that “Bank.Bank() is inaccessible due to its protection level“.

Private Constructors in C#

When can we make private constructor in C#?

Private Constructor is a special instance constructor available in C#. Normally, we create a private constructor of class in case of singleton pattern and another example is when we have all data members as static so that we can access those directly by the name of class only.

Can we create private constructor in abstract class?

Yes, it is feasible. We can create private constructor in abstract class.

How to get instance of class having Private Constructor?

Finally, we’ll explain below how to create the object of class having private constructor. For this we need to use “Activator” class.

In below image, first example shows how to create object of class having private constructor but using “Activator class”. Second way is the traditional way of creating object, but it will not allow you to create object if class will have private constructor.

9.Activator Class - https://codeconfig.in

You can find complete source code below. For additional information you can also view our video HERE:

namespace PrivateConstructor
{
    class Program
    {
        static void Main(string[] args)
        {
            //Way-1: creating object using Activator class
            Type type = typeof(Bank);
            Bank obj = (Bank)Activator.CreateInstance(type, true);
            obj.show();

            //Way-2: Creating object  
            //Bank obj = new Bank();    
            //obj.show();
            Console.ReadLine();
        }
    }
    public class Bank {
        private readonly int bankid;
        private Bank()
        {
            bankid = 101;
            Console.WriteLine("1.Private constructor called... "); 
        }
        public void show() {
            Console.WriteLine("2. Show method is called...");
        }
    }
}

For more detail checkout below Video:

In this article I tried to explain Private Constructors in C# with Examples. I hope you enjoyed reading this article. For more information you can check out Microsoft learn.

For more information you can visit our C# Section for more such articles.

Leave a Reply

Your email address will not be published. Required fields are marked *