What are public, private and static constructors and differences between them?

Showing Answers 1 - 4 of 4 Answers

Public constructors are used to instantiate and initialize the fields during object instantiation.

Private constructors are used to restrict the instatiation of object using 'new' operator. This type of constructors are mainly used for creating singleton object.

Static constructors are used when initializing class variables (static variables).

PeterVH

  • Aug 19th, 2011
 

Public constructors = free for all to use
private contructors = If there is danger in the contruction you use a factory pattern or static singleton pattern so that the class can assume responsability and lessen the risks involved
static contructor = Is called once, and in theory you don't know when (well, at least for you make the first instance). Use this for fixed values.

Code
  1. span style="color: #ff0000;">"Starting the function""in my testinstance, IsHiddenInitialized = " + toTest.IsHiddenInitialized());

  2.  

  3.             // guess not, now for the real deal

  4.             var realDeal = Example.GiveInstance("supersecret");

  5.             Debug.WriteLine("in my real deal, IsHiddenInitialized = " + realDeal.IsHiddenInitialized());

  6.  

  7.             /*

  8.              * output =

  9.              * Starting the function

  10.              * Static constructor is called

  11.              * 5

  12.              * in my testinstance, IsHiddenInitialized = False

  13.              * I am a nerd

  14.              * in my real deal, IsHiddenInitialized = True

  15.              * *//// <summary>

  16.         /// Default public contructor for testing purposes

  17.         /// </summary>

  18.         public Example()

  19.         {

  20.             // Setup some harmless test values

  21.         }

  22.  

  23.         /// <summary>

  24.         /// private constructor that does dangerous stuff and should be called only fron inside

  25.         /// this class, where the class itself can make sure nothing goes bad

  26.         /// </summary>

  27.         /// <param name="keepThisHidden">A secret to keep</param>/// <summary>

  28.         /// Static constructor to make sure the meaning of life is known

  29.         /// </summary>"Static constructor is called""supersecret" ?

  30.                 new Example("I am a nerd"

  Was this answer useful?  Yes

Constructor are used for creating an object and returning a reference of the object.
so whenever we write
class1 obj=new class1();

Public Constructor
The new Class1() is actually calling a public zero param constructor of class and instance is created.


Private Constructor
And if we mark the constructor as private we wont be able to write
class1 obj=new Class1()
hence the object of class1 cannot be created.
this type of construction is required when we are working with signleton pattern (Design Pattern)

Static Constructor
When we want our static data should be initiallize only one and first time we use
static constructor.
The Static constructor is called only once when the class or any of its derived is loaded in
clr before calling any instance constructor.

  Was this answer useful?  Yes

A public constructor is accessible anywhere in the application
A private constructor is restricted to the class within the application
Static constructor give access to the data without necessarily creating an object class

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions