Use of Private Constructor in C#

Use of Private Constructor in C# with an example

Use of Private Constructor in C#

Hey folks, till day you have read Difference between Throw and Throw ex in C#Difference between Var and Dynamic in c#,Difference Between WCF and WebAPIDifference between String and string in C#Difference between Nullable<> and ?,Difference between Hashtable and Dictionary,Insert data into Database table using SQLBulkCopy class in C#Ping in C# , Call method after specific time interval C#Insert Retrieve images from Database in asp.net LINQ TO SQL and Compare two objects in C# . Today we will discuss regarding Use of Private Constructor in C#.

There are basically two types of use of private constructor. Let’s take one by one use with an example and try to understand how can we take advantage of Private constructor in our programming.

Use-1: The class with a Private Constructor can’t be inherited.

using System;

namespace use_of_private_constructor_csharp
{
    class Program
    {
        static void Main(string[] args)
        {
        }
    }
    public class private_constructor_class
    {
        private private_constructor_class() { }
    }

    public class public_class : private_constructor_class
    {
    }
}

As shown in above code snippet we have created “private_constructor_class” class by using Private Constructor and then tried to inherit into public_class. But due protection of class by private constructor we couldn’t inherit that class into any other class and if we do so we could get compile time error as shown below image.

 

protection-inheritance-in-C#
protection-inheritance-in-C#

 

Use-2: Can’t allow to create an object of the class, which have Private Constructor or we must have public constructor with parameters.

using System;

namespace use_of_private_constructor_csharp
{
    class Program
    {
        static void Main(string[] args)
        {
            //Error occurred
            private_constructor_class obj = new private_constructor_class(); 
            //No Error
            private_constructor_class objNew = new private_constructor_class("Set Something");
            //Static Method could be called.   
            private_constructor_class.method1("Static method/members could be called only..");
        }
    }
   public class private_constructor_class
    {
        private private_constructor_class(){}

        public private_constructor_class(string setter) { }

       public static void method1(string message)
       {
           Console.WriteLine(message);
       }
    }
}

As shown in above code snippet we could conclude that we couldn’t create object class in which constructor is declared as Private. But if we have declared overloaded constructor then we could create object of that class by using overloaded method. We could directly access static members/methods of class which has private constructor declared which has been demonstrated in below image.

 

Initiate-private-Class-c#
Initiate-private-Class-c#

 

In a conclusion, try with your own by using given code snippet and try to understand by your own the uses of Private constructor. If you want to understand real implementation of Private Constructor, you will have to study design patterns like factory pattern.

 

Leave a Reply