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
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor
PREMIUM SPONSORS
  • ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications. Visit DynamicPDF here
    Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
Sponsored by
Team Foundation Server Hosting
Become a Sponsor
PRIVACY POLICY | TERMS & CONDITIONS | SITEMAP | CONTACT US | REPORT ABUSE © 2011 C# Corner. All contents are copyright of their authors.