Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Static Constructor in derived class getting invoked first then the base class

Tags:

c#

.net

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 ? :(

like image 984
Jasmine Avatar asked Jun 27 '12 19:06

Jasmine


1 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.

like image 124
ladenedge Avatar answered Sep 23 '22 06:09

ladenedge