Static Constructor

Posted on Updated on

Image result for Static ConstructorStatic Constructor

IN C# there are two types of constructor-

  • Class constructor (static constructor)
  • Instance constructor (non-static constructor)

Static constructor: Its main purpose is to initialize static data members as soon as the class is referenced first time. As static constructor is a class constructor, they are guaranteed to be called as soon as it refer to that class or by creating an instance of that class.

We may initialize static data members where we declare them in the code as-

privatestaticintid = 10;
privatestaticstring name = “jack”;

but there are times when value of one static member may depend upon the value of another static member. Such cases require some mechanism to handle conditional initialization of static members. To handlesuch a situation, static constructor are used.

Example:

//File Name : Test.cs
using System;
namespace Constructor
{
class Test
{
//Declaration and initialization of static data member
privatestaticint id = 5;
publicstaticint Id
{
get
{
return id;
}
}
publicstaticvoid print()
{
Console.WriteLine(“Test.id = ” + id);
}
staticvoid Main(string[] args)
{
//Print the value of id
Test.print();
}
}
}
In the above example, static data member <id> is declared and initialized in same line. So if code is compiled and run the program output would look similar to this :

Test.id = 5

Let’s create one more class similar to class Test but this time the value of its static data member would depend on the value of static data member <id> of class Test.id.

//File Name: Test1.cs
using System;
namespace Constructor
{
class Test1
{
privatestaticint id ;
//Static constructor, value of data member id is set conditionally here.
//This type of initialization is not possible at the time of declaration.
static Test1()
{
if( Test.Id< 10 )
{
id = 20;
}
else
{
id = 100;
}
Console.WriteLine(“Static<Class> Constructor for Class Test1 Called..”);
}
publicstaticvoid print()
{
Console.WriteLine(“Test1.id = ” + id);
}
staticvoid Main(string[] args)
{
//Print the value of id
Test1.print();
}
}
}

As in the above static constructor, static data member <id> is initialised conditionally. This type of initialisation is not possible at the time of declaration. So if this program is compiled and executed this will yield the output as:

Static<Class> Constructor for Class Test1 Called.
id = 20

Since <id> in class Test was initialized with a value of 5, therefore <id> in class Test1 got initialized to a value of 20.

Properties of static constructor

1) It executes before any instance of the class is created.
2) It executes before any of the static members for the class are referenced.
3) It executes after the static field initializers (if any) for the class.
4) It executes at most one time during a single program instantiation.
5) It does not take access modifiers or have parameters.
6) It is called automatically to initialize the class before the first instance is created or any static members are referenced.
7) They can’t be called directly.
8) The user has no control on when the static constructor is executed in the program.
9) 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.

10) Static constructors are also useful when creating wrapper classes for unmanaged code, when the constructor can call the LoadLibrary method.

11) 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.

*****************************************************************************************************

For more details and queries please feel free to email, visit or call us. Wishing you the very best for all your future endeavors.
Helpline: 9814666333, 8699444666
Email: info@technocampus.co.in

Please fill the form and we shall contact you soon.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s