static contructors real time advantage

Posted by Gayathri under C# on 7/10/2012 | Points: 10 | Views : 6906 | Status : [Member] | Replies : 1
I need an example for static constructors usage.
when and where static constructors are mainly used in real time coding.




Responses

Posted by: Premalatha on: 7/10/2012 [Member] Starter | Points: 25

Up
0
Down
A static constructor initializes static fields. It runs at an indeterminate time before those fields are used. Microsoft's documentation and many developers warn that static constructors on a type impose a substantial overhead. Static constructors-also called type initializers-initialize fields before they are accessed.
Example:

We see an example class with a static constructor, and also an example class without. Most classes you use will likely not have static constructors. My results were that static constructors do cause a slowdown of all accesses to the type. Note that instances are not the same as types, in that instances are specific objects of the type.
This C# example demonstrates static constructors. It benchmarks Them
Program that demonstrates static constructor [C#]
/// <summary>
/// This type has a static constructor.
/// </summary>
static class HasStaticConstructor
{
/// <summary>
/// Public field.
/// </summary>
public static int _test;

/// <summary>
/// Static constructor initializes public field.
/// </summary>
static HasStaticConstructor()
{
_test = 1;
}
}

/// <summary>
/// This type has no static constructor.
/// </summary>
static class NoStaticConstructor
{
/// <summary>
/// Public field initialized inline.
/// </summary>
public static int _test = 1;
}

class Program
{
static void Main()
{
System.Console.WriteLine(HasStaticConstructor._test);
System.Console.WriteLine(NoStaticConstructor._test);
}
}

Output

1
1

Benchmark notes

The value 2 is stored in the _test field in each iteration.

Benchmark results

HasStaticConstructor: 2.89 ns
NoStaticConstructor: 0.32 ns
Description. What the code does is compare a class that has a static, type constructor against one that does not. In the first class, which has the static constructor, the static field _test is initialized in the constructor.

Class without static constructor. In the second class, which has no static constructor, the _test field is initialized inline, meaning directly in the declaration. This has the same effect on behavior.
A static constructor is used to initialize any static data, or to perform a particular action that needs performed once only. It is called automatically before the first instance is created or any static members are referenced.

C#
class SimpleClass
{
// Static constructor
static SimpleClass()
{
//...
}
}

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.

Example
In this example, the class Bus has a static constructor and one static member, Drive(). When Drive() is called, the static constructor is invoked to initialize the class.

C#
public class Bus
{
// Static constructor:
static Bus()
{
System.Console.WriteLine("The static constructor invoked.");
}

public static void Drive()
{
System.Console.WriteLine("The Drive method invoked.");
}
}

class TestBus
{
static void Main()
{
Bus.Drive();
}
}

Premalatha
Software Engineer

Gayathri, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response