A constructor in C# is a special method of a class that is used to instantiate a class. A class constructor, just like other class methods, can be public, private, or protected. In this article, we will discuss scenarios where you can use a private constructor.

What is a Constructor?

As I said earlier, a constructor is a special method used to instantiate a class. A constructor has the same name as the class. For example, in the following script, you create an object of the Student class. Here while creating the object of the Student class, you are calling the constructor. The constructor in the following example is public.

class Program
    {
        static void Main(string[] args)
        {
            Student s = new Student();
            Console.ReadKey();
        }

    }

    public class Student
    {
        public static int age;

        //this is constructor

        public Student()
        {
            Console.WriteLine("This is a constructor");
        }
    }

Use a Private Constructor in C#

Uses of Private Constructor

Now you know what a constructor is, let’s discuss the uses of a private constructor.

There are three main uses:

  • When all the members of a class are static
  • When you don’t want a class to be inherited by other classes
  • When you want to implement singleton design pattern

Let’s see these scenarios in detail:

1. When All the Class Members are Static

A member of a class which can be a method or a variable can be static or non-static. A static member of a class can be accessed using the class name. You don’t need to instantiate a class in order to access its static member. Hence, if all the members of a class are static and can be accessed via the class name, you can add a private constructor to prevent class instantiation.

Here is an example. The Student class in the following script has two static members age and name. The class contains a private constructor. The member variables’ age and name are accessed via the Student class name and are printed on the console.

class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine(Student.age);
            Console.WriteLine(Student.name);
            Console.ReadKey();
        }
    }

    public class Student
    {
        public static int age = 10;
        public static string name = "James";

        //this is private constructor
        private Student()
        {
            Console.WriteLine("This is a constructor");
        }
    }

Here is the output of the above script:

Use a Private Constructor in C Sharp

Now if you try to create an object of the Student class, you will see an error as shown in the output of the following script:

class Program
    {
        static void Main(string[] args)
        {
            Student s = new Student();
            Console.ReadKey();
        }
    }

How to Use a Private Constructor in C Sharp

2. When You Don’t want a Class to Be Inherited

Adding a private constructor prevents a class from being inherited by other classes. In the script below, the Student class has a private constructor. The JuniorStudent class tries to inherit the Student class. Since the Student class has a private constructor, an exception is through as shown in the output of the script below:

    public class Student
    {
        public static int age = 10;
        public static string name = "James";

        //this is private constructor
        private Student()
        {
            Console.WriteLine("This is a constructor");
        }
    }

    public class JuniorStudent:Student
    {
        public static string department = "English";
    }

How to Use a Private Constructor in C#

3. While Implementing a Single Design Pattern 

You can use a private constructor when you want to implement a single design pattern. In a single design pattern, only one object of a particular class is created.

Let’s see an example of how a private constructor can help implement a singleton design pattern.

Look at the Student class in the script below. It has three static member variables: age, name, and s. The variable “s” is of type Student.

The Student class contains a private constructor and a static method getObject(). The getObject() method checks if the value of the “s” variable is null (Student class has not been initialized) or not. If the “s” variable is null, the getObject() method creates an object of the Student class and assigns it to the variable “s”. Else, if “s” already contains the object of the Student class, simply return it.

If you call the getObject() method of the Student class twice in the following script, you will see the same values for the age and name variable since on the second call to the getObject() method, no new Student class object is created and the already created object is returned.

class Program
    {
        static void Main(string[] args)
        {
            Student s_object = Student.getObject(24, "james");
            Console.WriteLine(s_object.age);
            Console.WriteLine(s_object.name);

            Student s_object2 = Student.getObject(15, "Nick");
            Console.WriteLine(s_object.age);
            Console.WriteLine(s_object.name);

            Console.ReadKey();
        }
    }

    public class Student
    {
        public  int age = 10;
        public string name = "James";
        public static Student s;

        //this is private constructor
        private Student()
        {
            Console.WriteLine("This is a constructor");
        }

        //this is a static method
        public static Student getObject(int age, string name)
        {
            if (s == null)
            {
                s = new Student();
                s.age = age;
                s.name = name;
            }

            return s;
        }
    }

Here is the output:

How to Use a Private Constructor in C