Click here to Skip to main content
15,867,765 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
what is the use of static constructor & static class please explain with example
Posted

hi,
Use a static class to contain methods that are not associated with a particular object. For example, it is a common requirement to create a set of methods that do not act on instance data and are not associated to a specific object in your code. You could use a static class to hold those methods.

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.

regards,
shefeek
 
Share this answer
 
Comments
manika123 22-Jul-11 1:58am    
Thanks ..... would u please give me one example of static constructor
lukeer 22-Jul-11 3:02am    
public class MyClass
{
public static string Answer = "Fourty-two";

public static MyClass()
{
Console.Writeline( "Executing static constructor.");
}
}

The above class has a static constructor. It's just like a normal constructor with the static keyword. It gets called before the first usage of the class.

In the given example, the only use the class could have is providing the "Answer" string. On request of this data, the static constructor is executed and after that, the answer is delivered.

Marking a class static does nothing but ensure at compile time that there are only static members and methods in it.
shefeekcm 22-Jul-11 3:35am    
are ou ok with this answer,if you need examples just comment on it.
Sergey Alexandrovich Kryukov 23-Jul-11 0:08am    
Detailed enough, my 5. As OP keeps asking about the use of static class, I added some information in my solution, please see.
--SA
 
Share this answer
 
 
Share this answer
 
Comments
manika123 22-Jul-11 1:51am    
Thanks Suresh its a gud stuff but why we use it ?
Static constructor is used to initialize static data members as soon as the class is referenced first time. Through this we can restrict creating multiple objects for a class.

Singleton pattern is the best example for this.

Best Example is Banking Application Software.
 
Share this answer
 
You already got enough about the nature of the static class and its static constructor. For the rest of my answer it's only important to understand that you can mark the class static only if all its members are static.

The uses? There are many uses; and giving many examples would hardly make much sense. If you understand how static members work you will understand why using them. Just one typical example: a utility class. Sometimes, you need just a collection of algorithms. The data is supplied only in the parameters of the methods. In this case, there is not need in instance data at all.

Another example is a collection of constants and read-only data members. Consider such static data class as a resource. Sometimes it is better than using a "real" resx resource.

Final example: create a resx resource file, add some resources. Visual Studio will generate source file you can used to access resources by using a static class and its properties in a safe way.

—SA
 
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