What is private constructor and what are some of it's benefits.

 Posted by Rajnilari2015 on 10/20/2016 | Category: C# Interview questions | Views: 2077 | Points: 40

Private Constructor is a special instance constructor used in class that contains static memory only.

The primary use of a private constructor is not to allow to create class instance when there are no instance fields or methods, or when a method is called to obtain an instance of a class.

Some benefits are

a) The constructor can only be accessed from static factory method inside the class itself e.g Singleton Class (where not more than one instance of the class exists in the program.)

b) Limit the number of object of a class

c) Preventing creating copy of the class (i.e. copy constructor)

d) A utility class that only contains static methods. (though here we can use a static class but it's an option)

e) Class cannot be inherited

Asked In: Many Interviews | Alert Moderator 

Comments or Responses

Login to post response