Sunday, September 30, 2007

Static Constructors

C# supports two types of constructor, a class constructor (static constructor) and an instance constructor (non-static constructor).
Static constructor is used to initialize static data members as soon as the class is referenced first time, whereas an instance constructor is used to create an instance of that class with keyword. A static constructor does not take access modifiers or have parameters and can't access any non-static data member of a class.

class StaticConstructor
{ static StaticConstructor()
{
Console.WriteLine("I am in static Constructor");
}
public StaticConstructor()
{
Console.WriteLine("I am in normal Constructor");
}
}
class Program
{
static void Main(string[] args)
{
StaticConstructor staticConstructor1 = new StaticConstructor();
StaticConstructor staticConstructor2 = new StaticConstructor();
Console.ReadLine();

}
}

No comments: