Static Constructor
Posted On Saturday, August 1, 2009 at at 2:32 PM by test
What is Static Constructor:
C# supports two types of Constructor
1.Class Constructor or Static Constructor-used to initialize static data members as soon as the class is referenced first time
2.Instance Constructor or non-Static Constructor-used to create an instance of that class with new keyword.
Static Data Member can be initialized at the time of their declaration(clsWithoutStaticConstructor in the following example) but there are times when value of one static member (clsWithStaticConstructor in the following example)may depend upon the value of another static member.
using System;
using System.Collections.Generic;
using System.Text;
namespace StaticConstructor
{
class clsWithoutStaticConstructor
{
private static int numStatData = 8;
public static int StatData
{
get { return numStatData; }
}
public static void print()
{
Console.WriteLine("Static Data :" + StatData);
}
}
class clsWithStaticConstructor
{
private static int numStatData2;
static clsWithStaticConstructor()
{
if (clsWithoutStaticConstructor.StatData < 10)
{
numStatData2 = 10;
}
else
{
numStatData2 = 100;
}
Console.WriteLine("Static Constructor is called...");
}
public static void print2()
{
Console.WriteLine("Static Data :" + numStatData2);
Console.ReadLine();
}
}
class Program
{
static void Main(string[] args)
{
clsWithoutStaticConstructor.print();
clsWithStaticConstructor.print2();
}
}
}
Output :
In the above example "numStatData2" in "clsWithStaticConstructor" is initialized based on the value of clsWithoutStaticConstructor.StatData .since StatData in the clsWithoutStaticConstructor is initilized to 8 so the value of numStatData2 is set to 10.
Static Constructor executes:
-before any instance of the class is created.
-before any of the static members for the class are referenced.
-after the static field initializers(if any) for the class.
-atmost one time.
C# supports two types of Constructor
1.Class Constructor or Static Constructor-used to initialize static data members as soon as the class is referenced first time
2.Instance Constructor or non-Static Constructor-used to create an instance of that class with new keyword.
Static Data Member can be initialized at the time of their declaration(clsWithoutStaticConstructor in the following example) but there are times when value of one static member (clsWithStaticConstructor in the following example)may depend upon the value of another static member.
using System;
using System.Collections.Generic;
using System.Text;
namespace StaticConstructor
{
class clsWithoutStaticConstructor
{
private static int numStatData = 8;
public static int StatData
{
get { return numStatData; }
}
public static void print()
{
Console.WriteLine("Static Data :" + StatData);
}
}
class clsWithStaticConstructor
{
private static int numStatData2;
static clsWithStaticConstructor()
{
if (clsWithoutStaticConstructor.StatData < 10)
{
numStatData2 = 10;
}
else
{
numStatData2 = 100;
}
Console.WriteLine("Static Constructor is called...");
}
public static void print2()
{
Console.WriteLine("Static Data :" + numStatData2);
Console.ReadLine();
}
}
class Program
{
static void Main(string[] args)
{
clsWithoutStaticConstructor.print();
clsWithStaticConstructor.print2();
}
}
}
Output :
In the above example "numStatData2" in "clsWithStaticConstructor" is initialized based on the value of clsWithoutStaticConstructor.StatData .since StatData in the clsWithoutStaticConstructor is initilized to 8 so the value of numStatData2 is set to 10.
Static Constructor executes:
-before any instance of the class is created.
-before any of the static members for the class are referenced.
-after the static field initializers(if any) for the class.
-atmost one time.