E
Elia Karagiannis
The static constructor of a derived class never gets called if it has no
other methods and the base class is only static
I have the following base class and derived class.
public class MyBase
{
protected MyBase()
{}
protected static int x;
public static void DoSomething()
{
Console.WriteLine("X = " + x.ToString());
}
}
// the derived class
public class MyDerived : MyBase
{
static MyDerived()
{
x = 5;
}
}
static void Main()
{
MyDerived.DoSomething(); // prints out zero instead of 5
// the static
constructor on the derived class never gets called
// if I add a dummy
static method to the derived class and call it then the
// static
constructor of the derived class will be called but I cant do this...
}
Any suggestions or can this just not be done??
other methods and the base class is only static
I have the following base class and derived class.
public class MyBase
{
protected MyBase()
{}
protected static int x;
public static void DoSomething()
{
Console.WriteLine("X = " + x.ToString());
}
}
// the derived class
public class MyDerived : MyBase
{
static MyDerived()
{
x = 5;
}
}
static void Main()
{
MyDerived.DoSomething(); // prints out zero instead of 5
// the static
constructor on the derived class never gets called
// if I add a dummy
static method to the derived class and call it then the
// static
constructor of the derived class will be called but I cant do this...
}
Any suggestions or can this just not be done??