Interviews - C# Corner

Urvi Solanki
Why private constructor is used ?
By Urvi Solanki in C# onJun 20 2018
  • Jignesh Kumar
    Jul, 2018 2

    Two thing to notice about private constructor :1. Class with private constructor can not be inherited 2. We can not create object of the class which has private constructorUse of Private constructor: Many times we do not want create instance of some of the classes like Helper, utility and common routine classes.

    • 8
  • Pravin Lalge
    Jul, 2018 20

    1. Private Constructor: Private Constructors are used to restrict the instantiation of object using 'new' operator.  2. It is commonly used in classes that contain static members only.  3. This type of constructors is mainly used for creating singleton object.  4. If you don't want the class to be inherited we declare its constructor private.  5. We can't initialize the class outside the class or the instance of class can't be created outside if its constructor is declared private. 6.  We have to take help of nested class (Inner Class) or static method to initialize a class having private constructor.

    • 3
  • Appsierra Solutions
    Jun, 2018 26

    private constructor don`t allow users to create an instance of class , mainly we are using private constructor whenever we need singleton class.

    • 2
  • Harini R
    Aug, 2018 2

    Private constructors are used to prevent creating instances of a class when there are no instance fields or methods, such as the Math class, or when a method is called to obtain an instance of a class. If all the methods in the class are static, consider making the complete class static. For more information see Static Classes and Static Class Members.

    • 1
  • Harini R
    Aug, 2018 2

    Private constructors are used to prevent creating instances of a class when there are no instance fields or methods, such as the Math class, or when a method is called to obtain an instance of a class. If all the methods in the class are static, consider making the complete class static. For more information see Static Classes and Static Class Members.

    • 1
  • Harini R
    Aug, 2018 2

    Private constructors are used to prevent creating instances of a class when there are no instance fields or methods, such as the Math class, or when a method is called to obtain an instance of a class. If all the methods in the class are static, consider making the complete class static. For more information see Static Classes and Static Class Members.

    • 1
  • Pravin Lalge
    Jul, 2018 20

    1. Private Constructor: Private Constructors are used to restrict the instantiation of object using 'new' operator.  2. It is commonly used in classes that contain static members only.  3. This type of constructors is mainly used for creating singleton object.  4. If you don't want the class to be inherited we declare its constructor private.  5. We can't initialize the class outside the class or the instance of class can't be created outside if its constructor is declared private. 6.  We have to take help of nested class (Inner Class) or static method to initialize a class having private constructor.

    • 1
  • Sanghamitra Mohanty
    Jun, 2018 29

    The constructor can only be accessed from static factory method inside the class itself. Singleton can also belong to this category. A utility class, that only contains static methods.Generally, they are used in singleton design patterns, where the code ensures that only one instance of a class can ever be created.

    • 1
  • Shivam Shukla
    Jun, 2018 27

    First of all, .subscribe is not an Angular2 thing.That's a method that comes from rxjs library which Angular is using behind the scene.If you can imagine yourself when subscribing to a newsletter and after the subscribing, every time that there is a new newsletter, they will send it to your home ( the method inside subscribe get's called).That's what happens when you subscribing to a source of magazines ( which they call it Observable in rxjs library)All the AJAX calls in Angular is using this library behind the scene and in order to use any of them, you've got to use the method name, e.g get, and then call subscribe on it, because get returns and Observable.Also, when you're doing this

  • Deepak Verma
    Jun, 2018 26

    The private constructor is generally used in classes that contain static members only and also used to prevent creating instances of a class when there are no instance fields or methods.

    • 1
  • ajay garde
    Jun, 2018 25

    Private constructors are used to prevent creating instances of a class when there are no instance fields or methods, such as the Math class, or when a method is called to obtain an instance of a class.

    • 1
  • Saurabh Vasani
    Jul, 2019 26

    Main use of creating Private constructor is that we cant create the object of that class which contains Private Constructor and the another use is that you can not inherit that class. Private constructor is also use to implement singleton pettern where you just want create the object of the class just once throughout the execution of the application.

    • 0
  • Amit Prakash
    Jan, 2019 2

    Two thing to notice about private constructor :1. Class with private constructor can not be inherited 2. We can not create object of the class which has private constructorUse of Private constructor: Many times we do not want create instance of some of the classes like Helper, utility and common routine classes.

    • 0
  • Siddhesh Chalke
    Oct, 2018 29

    private constructor restricts developers from creating instance outside of that class. Thus can be used to implement "Singleton" design pattern.

    • 0
  • JITENDRA SHARMA
    Aug, 2018 20

    private constructor is used for limit instantiation...or stop the instantiation..

    • 0
  • Harini R
    Aug, 2018 2

    Private constructors are used to prevent creating instances of a class when there are no instance fields or methods, such as the Math class, or when a method is called to obtain an instance of a class. If all the methods in the class are static, consider making the complete class static. For more information see Static Classes and Static Class Members.

    • 0
  • Chandan Prasad
    Jul, 2018 27

    the main advantage of private constructor is to prevent the instantiation of class and also we use private constructor when we implement singleton pattern and also when we have helper classes or utility class in those we cases we don't to create instance of class rather we want to call the class by name

    • 0
  • shalini sharma
    Jul, 2018 24

    Private constructor:- 1.it is use in singleton design pattern in which create single object that is in it. 2.Class with private constructor cannot be inherited.3.also cannot create instance in another class.

    • 0
  • Kashif Asif
    Jul, 2018 6

    1- It doesn't allow developer to create Class Constructor. 2- When you want required class should be inhered

    • 0
  • Shrimant Telgave
    Jul, 2018 5

    Use of private constructor: 1. To prevent creating instances of a class when there are no instance fields or methods, such as the Math class. 2. Use if all the methods in the class are static, consider making the complete class static.

    • 0
  • Abhijit Sahoo
    Jul, 2018 3

    When you don't want users to create instances of your class, then create private constructor of that class. Then how to use that class? Make a static function/property/field which will return instance of that class, and return it.

    • 0

Most Popular Companies

Most Popular Job Functions

MOST LIKED QUESTIONS