Skip to main content

Posts

Showing posts from January, 2017

WHY ISN’T THE STATIC CONSTRUCTOR FROM MY BASE CLASS CALLED?

Lets say we have 2 classes: public abstract class Foo { static Foo () { print ( "4" ); } } public class Bar : Foo { static Bar () { print ( "2" ); } static void DoSomething () { /*...*/ } } We expected that after calling  Bar.DoSomething()  (assuming this is the first time we access the Bar class) the order of event will be: Foo’s static constructor (again, assuming first access) > print  4 Bar’s static constructor > print  2 Execution of  DoSomething At the bottom line we expect  42  to be printed. After testing, it seems that only  2  is being printed. Reason : The static constructor for a class executes at most once in a given application domain. The execution of a static constructor is triggered by the first of the following events to occur within an application domain: An instance of the class is created. Any of the static members of the class are