Thursday, May 26, 2011

Static Constructors

/*
 *  Non-static classes can have both static and non static constructors.
 *  A non-static class must have a non-static default constructor. If it is not provided, it is provided
 *  by the compiler.
 *  A non-static class may optionally declare a  static constructor. It will be called before the first instance
 *  of the non-static class is created. The static constructor can only be used to initialize static members.
 *  If a non-static class  declares a static constructor only,
 *  then the compiler provides a default parameterless non-static constructor.

 * * Access modifiers are not allowed on static constructors. (public/private/etc), because they are called by the framework. They are not
 * user callable (cannot be called explicitely).
 *
 *
 *  Static constructors cannot act on instance members, because static constructor is always called before any instance of
 *  the class exists.
 *  Static constructors are called once only, they are not called on every instance creation.
 *  A class's static members cannot be accessed with an  instance reference, they must be qualified with a type name instead.
 * 
 * You can have two parameterless constructors for a class , one static and one non-static. They will have same signatures
 * including same name, except one will have static keyword and no access specifier. However, you CANNOT have two methods with
 * same name and signatures, with one as static and one non-static.
 *
 * Static members (data members) persist their values across all instances of the class.
 * Static methods can be accessed without instantiating the class.
 *
 *
 *
 * MSDN==================================================================
 * Link : http://msdn.microsoft.com/en-us/library/k9x6w0hc(v=VS.100).aspx
 *
 * Static constructors have the following properties:
A static constructor does not take access modifiers or have parameters.
A static constructor is called automatically to initialize the class before the first instance is created or any static members are referenced.
A static constructor cannot be called directly.
The user has no control on when the static constructor is executed in the program.
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.
Static constructors are also useful when creating wrapper classes for unmanaged code, when the constructor can call the LoadLibrary method.
If a static constructor throws an exception, the runtime will not invoke it a second time, and the type will remain uninitialized for the lifetime of the application domain in which your program is running.
 * MSDN==================================================================
 */



A very good article on constructors can be found  at:

http://dhondiyals.wordpress.com/2010/02/25/constructor-in-depth/




Some excertps from this article :

Constructors:
- Constructors cannot be “virtual”.
- They cannot be inherited.
- Constructors are called in the order of inheritance.
- Constructors can be overloaded.
- special function that is called when class is instantiated.
- same name as the class.
- does not have a return type.
- used to initialize values.
- can have parameters.
- compiler will supply one if not declared.

As per the C# specification, a constructor can have the following modifiers.
public
protected
internal
private
extern

There are three types of constructors in C#
a.Instance Constructors
b.Private Constructors
c.Static Constructors


A static constructor is used to initialize a class.It is not used to initialize instance of a class.
1. A static constructor must always be parameterless.
2. Since it cannot have parameters,a static constructor cannot be overloaded.
3. Access modifiers are not allowed on static constructors. 
4. A Static constructor cannot be inherited.
5. A static constructor cannot be called explicitly.It is called automatically(implicitly). If a class does not define the entry point Main() method,then the execution of a static constructor is triggered automatically by the first of the following events to occur within an application domain.   
 (Case – A) An instance of the class is created.
 (Case – B) Any of the static members of the class are referenced.
6. If a class contains the entry point Main() method,then the static constructor for that class executes automatically before the Main method is called within an application domain.
7. The static constructor for a class executes at most once in a given application domain.
8. If a class contains any static fields with initializers, those initializers
are executed prior to the execution of static constructor.
9. If a class contains any static fields with out initializers,then those static fields are initialized with default values prior to the execution of static constructor.
See the example of 5
The class AClass of example 5 has a static field ‘aStaticField’. ‘aStaticField’ is not intialized explicitly. So it is automatically intialized with the  default value 0 prior to the execution of static constructor
10. Like static methods, we cannot access a nonstatic field,method or property with in a
static constructor.
11. Static Constructor Executes Before an Instance Constructor
12. Static Constructor Can Access Only Static Members

No comments:

Post a Comment

How to check local and global angular versions

 Use the command ng version (or ng v ) to find the version of Angular CLI in the current folder. Run it outside of the Angular project, to f...