Private and Static Constructors in C#
C#

Private and Static Constructors in C#

In this post, I will cover Private and Static Constructors in C#. Private constructors restrict the creation of objects. In fact, if the private constructor is there, you cannot create objects in a similar fashion. You can only use private constructors inside some method of that class only. In contrast, if you define a constructor as static then you can create the objects anywhere in the program, where the defining class is also visible.

Static constructors are also called in a different manner. The invokation of a static constructor is always implicit. The programmer has no control over when the static constructor is called. However, we define a static constructor in a class, it will be executed first before any other constructor is executed. Secondly, static constructors can initialize only static members. If you have non-static variables in the class, then only a non-static constructor can initialize these variables. Also, you can have only one static constructor in a class. This is because the static constructor doesn’t take any parameter. Hence, it is not possible to overload a static constructor.

Private Constructors

Private constructors are those constructors which use the private access modifier. However, if you don’t use any access-modifier with constructot, then also it becomes a private constructor. Basically, if a class has only private constructor, then you can’t instantiate it. Consider following example,

using System;
namespace PrivateConstructorDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            MyClass ob = new MyClass();
        }
    }
    class MyClass
    {
        private MyClass() { }
    }

}

The above code will result in a compile-time error. If you define the class as given below, the constructor will still remain private and the program will not compile.

 class MyClass
    {
        MyClass() { }
    }

Suppose, you define both private and public constructors in a class, then you can create objects with the help of publicly available constructors as shown below.

using System;
namespace PrivateConstructorDemo
{
    class Program
    {
        static void Main(string[] args)
        {
         //   MyClass ob = new MyClass();
            MyClass ob1 = new MyClass(10, 20);
        }
    }
    class MyClass
    {
        MyClass() { }
        public MyClass(int a, int b)
        {
            Console.WriteLine(a + b);
        }
    }
}

Output:

Using Both Private and Public Constructors
Using Both Private and Public Constructors

Important Points about Private Constructors

  • If a class has only private constructors, then it can not be instantiated. It means you cannot create any object of that class outside the definition of the class.
  • You can define both private and public constructors in the same class. In this case, you can instantiate the class with the help of public constructors only.
  • If a class contains only private constructors, then it cannot be inherited as illustrated in the following code.
using System;
namespace PrivateConstructorDemo
{
    class Program
    {
        static void Main(string[] args)
        {
        }
    }
    class MyClass
    {
        MyClass() { }
    }
    class SubClass : MyClass
{ }
}

The above code will give a compile-time error.

There are many situations when we want to have only private constructors in a class. To find out more, read this post on When Should We Use Private Constructors?

Factory Methods

Private constructors can also be used by the static factory methods to create an instance of the class.

Static Constructors in C#

A C# class can also have a static constructors. Basically, the static constructors are defined with the static keyword. Also, they are called before any other constructor is called. The important points related to static constructors are given below.

  • We define the static constructors with the static keyword.
  • A class can have only one static constructor.
  • You can not pass a parameter to the static constructor.
  • Usually, the static constructors initialize the static fields.
  • Static constructors don’t have any access-modifier.
  • They are invoked before any other constructor of the class is called.

You can find several examples of static constructors in this post on Examples of Static Constructors in C#.

To summarize, in this post, we learned Private and Static Constructors in C#. The private constructors restrict the creation of objects of a class. On the other hand, the static constructors initialize the static members of the class.

Leave a Reply

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