Lets say I have 2 classes:
public abstract class Foo
{
static Foo()
{
print("4");
}
}
public class Bar : Foo
{
static Bar()
{
print("2");
}
static void DoSomething()
{
/*...*/
}
}
I expected that after calling Bar.DoSomething()
(assuming this is the first time I access the Bar class) the order of event will be:
4
2
DoSomething
At the bottom line I expect 42
to be printed.
After testing, it seems that only 2
is being printed.
And that is not even an answer.
Can you explain this behavior?
A static constructor is used to initialize any static data, or to perform a particular action that needs to be performed only once. It is called automatically before the first instance is created or any static members are referenced.
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.
Whenever the derived class's default constructor is called, the base class's default constructor is called automatically. To call the parameterized constructor of base class inside the parameterized constructor of sub class, we have to mention it explicitly.
A static constructor must be parameterless because nothing ever calls it, it is invoked when you access a static member or create an instance of the class, but not directly (it is called by the runtime).
The specification states:
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 referenced.
Because you are not referencing any of the members of the base class, the constructor is not being excecuted.
Try this:
public abstract class Foo
{
static Foo()
{
Console.Write("4");
}
protected internal static void Baz()
{
// I don't do anything but am called in inherited classes'
// constructors to call the Foo constructor
}
}
public class Bar : Foo
{
static Bar()
{
Foo.Baz();
Console.Write("2");
}
public static void DoSomething()
{
/*...*/
}
}
For more information:
The reason base class static constructor is not called is because, we are not yet accessing any static members of the base class nor creating the instance of the derived class.
As per documentation these are the times when a static constructor will be called.
It is called automatically before the first instance is created or any static members are referenced.
In the below code when we invoke DoSomething
the base class constructor will be called
public abstract class Foo
{
static Foo()
{
Console.Write("4");
j = 5;
}
protected static int j;
public static void DoNothing()
{
}
}
public class Bar : Foo
{
static Bar()
{
Console.Write("2");
}
public static void DoSomething()
{
Console.Write(j);
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With