sudhir kumar
What is difference between Private and Static Constructor?
Posted by sudhir kumar in OOP | Others on Oct 17, 2011
0
Do you know the answer for this question? Post it below.
Guest

1)A static constructor is called before the first instance is created. i.e. global initializer.

Whereas Private constructor is called after the instance of the class is created.

2)Static constructor will be called first time when the class is referenced. Static constructor is used to initialize static members of the class.

  Static members will not be initialized either by private or public constructor.

3)The static constructor will only be executed once.

  The private constructor will be executed each time it is called.

Posted by Rambabu CH on Oct 20, 2011

In my opinion, private are used with in the class only where it was declared and static methods can be used any where by using static member.

Posted by shyam panwar on Oct 19, 2011

Sorry to all, I made a mistake on my previous code in line 7. Please correct this.
For example:-
Class A
{
      int a;
      private A()
      {             
       }
       public A(int b) : this()  // Calling private constructor by another constructor.
       {
                this.a=b;
        }
}

Posted by sudhir kumar on Oct 18, 2011

Normally one uses the private constructor to create a singleton instance of a class, 

Static constructor is called only once in during application lifetime, so its a good place to initialize any static members of the class.

Posted by raghavendra setty on Oct 18, 2011

1. Static constructor is called before the first instance of class is created, wheras private constructor is called after the first instance of class is created.

2. Static constructor will be executed only once, whereas private constructor is executed everytime, whenever it is called.

Posted by uttam chaturvedi on Oct 17, 2011
Sponsored by
Team Foundation Server Hosting
Become a Sponsor
PREMIUM SPONSORS
Sponsored by
Nevron Gauge for SharePoint
Become a Sponsor
PRIVACY POLICY | TERMS & CONDITIONS | SITEMAP | CONTACT US | REPORT ABUSE © 2011 C# Corner. All contents are copyright of their authors.