Wednesday, January 1, 2014

Static Constructor

Key points about static constructor
1. It can only access the static member(s) of the class.
Reason : Non static member is specific to the object instance. If static constructor are allowed to work on non static members it will reflect the changes in all the object instance, which is impractical.

2. There should be no parameter(s) in static constructor.
Reason: Since, It is going to be called by CLR, nobody can pass the parameter to it.

3. Only one static constructor is allowed.
Reason: Overloading needs the two methods to be different in terms of method/constructor definition which is not possible in static constructor.

4. There should be no access modifier to it.
Reason: Again the reason is same call to static constructor is made by CLR and not by the object, no need to have access modifier to it

5. A class can contain both constructor static and non-static ate same time.
6. A static constructor called automatically.
7. A static constructor can not be called explicitly.
8. Static constructor used to initialize the static variables.
9. Static constructor are useful for creating wrapper classes for managed code. You can call Loadlibrary() function inside static constructor.
10. An Abstract class can contain static constructor.

No comments:

Followers

Link