Click here to Skip to main content
15,914,419 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
hello sir., I want to know about why use static constructor and what is the purpose of this constructor
Posted
Updated 26-Nov-18 4:30am

A static constructor is used to initialize any static data, or to perform a particular action that needs performed once only.
It is called automatically before the first instance is created or any static members are referenced.


You can find more from here.
http://msdn.microsoft.com/en-us/library/k9x6w0hc(v=vs.80).aspx[^]

http://www.dotnetperls.com/static-constructor[^]
 
Share this answer
 
Some important point regarding static constructor from C# Language Specification and C# Programmer's Reference :

1) The static constructor for a class executes before any instance of the class is created.
2) The static constructor for a class executes before any of the static members for the class are referenced.
3) The static constructor for a class executes after the static field initializers (if any) for the class.
4) The static constructor for a class executes at most one time during a single program instantiation
5) A static constructor does not take access modifiers or have parameters.
6) A static constructor is called automatically to initialize the class before the first instance is created or any static members are referenced.
7) A static constructor cannot be called directly.
8) The user has no control on when the static constructor is executed in the program.
9) A typical use of static constructors is when the class is using a log file and the constructor is used to write entries to this file.

Reference Link :- Static Constructor in C# and their Usages.[^]

and try also for further reference
http://msdn.microsoft.com/en-us/library/aa645612(v=vs.71).aspx[^]
http://www.dotnetperls.com/static-constructor[^]
 
Share this answer
 
Comments
koolprasad2003 24-Nov-11 1:27am    
nice explanation
RaviRanjanKr 24-Nov-11 1:31am    
Thanks :)
RaisKazi 24-Nov-11 2:32am    
Comprehensive, 5ed.
RaviRanjanKr 24-Nov-11 2:37am    
Thanks :)
Ayesha_K 17-Oct-15 2:04am    
how can we use it when the log file is being used ?? can you please further elaborate your example
What is: A static constructor is typically used to
initialize features that apply to a class rather than an instance. Thus, it is used to initialize aspects of a class before any objects of the class are created.

Points to be noted:

1.The static constructor is called automatically (when the class is first loaded) and before the instance constructor.

2.The static constructors cannot have access modifiers (thus, they use default access) and cannot be called by your program.

Usage:

1.A typical use of static constructors is when the class is using a log file and the constructor is used to write entries to this file.

2.Suppose we have a static class that uses database access then a static constructor is a handy place to initialize that database connection, because it means that the connection will get created right before it is first needed.

3.As you know each object occupies some space in memory. But by using static constructor we can reduce it. That means increase in performance.

hope it helps :)

for further queries comment here!
 
Share this answer
 
Comments
RaisKazi 24-Nov-11 2:33am    
Comprehensive, 5ed.
Uday P.Singh 24-Nov-11 4:48am    
thank you RaisKazi :)
Ayesha_K 17-Oct-15 2:06am    
the database access example is pretty much helpful . can you please give some more real time example so that it may clear in my mind
A static constructor is called automatically to initialize the class before the first instance is created or any static members are referenced



Check Google Results
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900