By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
426,248 Members | 1,979 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 426,248 IT Pros & Developers. It's quick & easy.

static constructor in derived class not being called

P: n/a
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??
Nov 17 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Elia,

Interesting stuff!

My guess is:
a) Before the call to MyDerived.DoSomething(), the CLR loads MyDerived and
immediately executes static constructor
b) Because of the inheritance relationship, the CLR loads MyBase, and
immediately initializes static field to 0 and then executes static
constructor (which in this case does nothing).
c) The call to DoSomething() proceeds, printing 0...

Regards - Octavio

"Elia Karagiannis" <ek**********@paperwise.com> escribió en el mensaje
news:uY**************@TK2MSFTNGP15.phx.gbl...
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??

Nov 17 '05 #2

P: n/a
Elia Karagiannis wrote:
Any suggestions or can this just not be done??


I guess this is as intended. The description in the C# language specs
(http://tinyurl.com/8wlg5) says:

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

Now, the problem here is that while you call the static method on the
class MyDerived, it's still not a "static member of the class", at least
not of *that* class.

Although that sounds confusing given the fact that it's possible to call
the base class static method via the derived class, I'm quite sure
that's how .NET sees it.
Oliver Sturm
--
omnibus ex nihilo ducendis sufficit unum
Spaces inserted to prevent google email destruction:
MSN oliver @ sturmnet.org Jabber sturm @ amessage.de
ICQ 27142619 http://www.sturmnet.org/blog
Nov 17 '05 #3

P: n/a
Sorry,

I thought DoSomething was defined in MyDerived...
Now I think I agree with Oliver.

Regards - Octavio

"Octavio Hernandez" <do****@danysoft.com> escribió en el mensaje
news:Om**************@TK2MSFTNGP09.phx.gbl...
Elia,

Interesting stuff!

My guess is:
a) Before the call to MyDerived.DoSomething(), the CLR loads MyDerived and
immediately executes static constructor
b) Because of the inheritance relationship, the CLR loads MyBase, and
immediately initializes static field to 0 and then executes static
constructor (which in this case does nothing).
c) The call to DoSomething() proceeds, printing 0...

Regards - Octavio

"Elia Karagiannis" <ek**********@paperwise.com> escribió en el mensaje
news:uY**************@TK2MSFTNGP15.phx.gbl...
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??


Nov 17 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.