1

I know the concepts of static constructor.

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.

I want to know why class contains single static constructor only? What is the real time scenario for static constructor ?

How it differs from private constructor? I googled lot of links, but I can't get good idea.

http://social.msdn.microsoft.com/Forums/en-US/a9f8dcca-32d1-4a2b-b3fe-7d8f34f3b3f1/c-programmingstatic-constructor

Private vs Static constructors in .Net

Community
  • 1
  • 1
SivaRajini
  • 7,225
  • 21
  • 81
  • 128

2 Answers2

3

i want to know why class contains single static constructor only?

Because it's being called automatically and there is no way to pass any parameter to that constructor. That's why only one, parameterless static constructor is possible.

what is the real time scenario for static constructor ?

You should use it for any work that has to be done before class is used and that has to be done only once.

how it differs from private constructor?

Private constructor is being run when you want it to run. Static constructor is run by CLR before class is used for the first time and you can determine when it happens.

And real-code example of static constructor usage - it creates an Expression Tree and compiles it to be used later and safe Expression Tree compilation from executing every time TestFunction is called:

class Test<T> where T : struct, IConvertible
{
    private static Func<int, T> _getInt;

    static Test()
    {
        var param = Expression.Parameter(typeof(int), "x");
        UnaryExpression body = Expression.Convert(param, typeof(T));
        _getInt = Expression.Lambda<Func<int, T>>(body, param).Compile();
    }

    public static T TestFunction(T x)
    {
        int n = Convert.ToInt32(x);
        T result = _getInt(n);
        return result;
    }
}

Code from Convert class, IConvertible interface and Generics performance comparison test

MarcinJuraszek
  • 124,003
  • 15
  • 196
  • 263
  • @Marchin thanks for your solution. private constructor generally used to avoid creating the instance for the class. but your feedback is "Private constructor is being run every time new class/struct instance is initialized". – SivaRajini Sep 02 '13 at 06:56
0

if you want to compare static constructor with instance constructor you can think about it in such way.

Instance constructor is used to instantiate a new instance of particular class (it's no matter if its public, private or protected).

Static constructor is used to "initialize" a whole type. That's why a static constructor is called automatically before the first instance is created or any static members are referenced. Semantically it is just type initialization piece of code not the constructor (or you can treat it as type constructor).

Private constructor are just instance constructors with private access visibility (so you can use it to instantiate new instances just inside the same class where it is declared).