I am wondering, generally in C#, the constructor concept is, base class cons should execute first, but why is that I am seeing derived class static constructor getting called and then base class cons. Could someone please explain ? :(
3 Answers
Static constructors initialize the class itself, which is to say that they must be called before any other static members are accessed, and before the creation of any instances of the class.
As for the ordering of calls to static constructors within a class hierarchy, you should consider that undefined. From the MSDN page on static constructors:
The user has no control on when the static constructor is executed in the program.

- 13,197
- 11
- 60
- 117
-
Thank you. But I see that derived class static cons is called derived d = new base(); where base is abstract. I am wondering why even though I call Base b = new Base(); and b.Sub(); – Jasmine Jun 27 '12 at 19:47
-
@Divine: sorry, I'm not sure I understand what you mean. Note that for class hierarchy `class B : A { }`, static constructors for both *B* and *A* must be called (in an undefined order) before either an *A* or a *B* can be instantiated. When it comes to accessing static members, on the other hand, static constructors are only guaranteed to be called *for the class containing the static member*. See also: [What's the best way to ensure a base class's static constructor is called?](http://stackoverflow.com/questions/4652454). – ladenedge Jun 27 '12 at 19:59
-
(in an undefined order) made my clarification solved. Now I am clear and you understood my question very correctly/ I got what I was exactly expecting. Sorry, I am little poor in communication :) Cheers. Thank you so much.... – Jasmine Jun 27 '12 at 20:01
Well, that's the whole point of static constructors; it has nothing to do with inheritance.
To quote MSDN
A static constructor is called automatically to initialize the class before the first instance is created or any static members are referenced.
You can declare their body, but don't need to worry about when they will be called (nor does the framework give you any guarantee in that regard, except that it will run before any instance of the class at hand has been created).
Edit
Oh, there is something else you should be aware of, and it has to do with generics, even though it might be obvious.
Consider this snippet:
class Foo<T> {
static Foo() {
Console.WriteLine("Danger, Will Robinson!");
}
}
Here the static constructor will be executed for whatever T
, because of course:
typeof(Foo<Bar>) != typeof(Foo<Baz>)

- 7,895
- 2
- 38
- 46
-
I see that always I get output like "I am from derived", "I am from base" when I actually create statements like this : derived d = new base(); where base is abstract class. – Jasmine Jun 27 '12 at 19:44
-
-
I then changed like this : base b = new base(); and then b.Add(); Still I see derived static constructor puts the contents first in console :( Whyyy – Jasmine Jun 27 '12 at 19:48
-
Uhmm Base b = new derived(); sorry....this way... I am instantiating.... to understand concepts of override and new.... – Jasmine Jun 27 '12 at 19:50
-
1) post some code, for Pete's sake. 2) and even more important: we already told you why the static constructor executes first: it's what it is designed to do, so that you know where to execute initialization tasks that should occur once and only once. – s.m. Jun 27 '12 at 19:53
-
Buddy yeah I understand, but my question is why the static constructor in derived class gets executed first and not the static constructor from base class ? Meaning no logic behind ? Its decided by CLR at runtime ? But I am always seeing derived static class gets instantiated.... If my understanding is correct I am happy then.... But I see that there should be some proper justified logic to execute the static constructor hierarchy like normal constructors.. – Jasmine Jun 27 '12 at 19:59
-
Thank you Sir, now I got a fair idea about the mechanism behind it... Thank you so much for helping me... I appreciate it...cheers – Jasmine Jun 27 '12 at 20:04
-
Ah, I didn't understand what you meant, it's difficult without the source code. Now that I know what you want to know, I can point you to [this answer](http://stackoverflow.com/a/4652610/129782) by Marc Gravell – s.m. Jun 27 '12 at 20:05
-
In other words, my friend, I'm afraid you are about to open the proverbial can of worms ;) – s.m. Jun 27 '12 at 20:06
-
Kudos to you, yeup sorry lol I am very bad in communication (English), sorry for the trouble caused...... Yeup I did go through your link, it was exactly the thing that I was trying in my VS too now :) Thank you soo much buddy :) – Jasmine Jun 27 '12 at 20:17
-
Static constructor of Derived class is called first because it is loaded before Base class. Constructor is called when the class is loaded into the memory.

- 1