What is Private Constructor? And it’s use? Can you create instance of a class which has Private Constructor?
By in OOP/OOD on Aug 03 2006
  • Ganesh Dutt Upadhyay
    Jun, 2007 10

    In C#, constructors can be declared as public, private, protected or internal. When a class declares only private constructors, it is not possible other classes to derive from this class or create an instance of this class. Private constructors are commonly used in classes that contain only static members.

    • 0
  • Ravi Kumar
    Nov, 2006 14

    When a class declares only private instance constructors, it is not possible for classes outside the program to derive from the class or to directly create instances of it. (Except Nested classes)
    Make a constructor private if:
    1. You want it to be available only to the class itself. For example, you might have a special constructor used only in the implementation of your class' Clone method.
    2.You do not want instances of your component to be created. For example, you may have a class containing nothing but Shared utility functions, and no instance data. Creating instances of the class would waste memory.

    • 0


Most Popular Job Functions


MOST LIKED QUESTIONS