473,399 Members | 3,832 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 software developers and data experts.

static constructor in derived class not being called

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
3 2903
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Ernst Murnleitner | last post by:
Hello Readers, Is there a way that only one class can construct a class A and its inherited classes A2, A3 etc.? I want to construct a class A (and the inherited classes A2, A3 etc.) from a...
12
by: cppaddict | last post by:
Hi, I know that it is illegal in C++ to have a static pure virtual method, but it seems something like this would be useful when the following 2 conditions hold: 1. You know that every one...
3
by: Kirk Marple | last post by:
Just want to see if this is 'by design' or a bug... I have a common List<T> defined in a base class, and the base class has a static property to expose this list. I wanted the derived class to...
17
by: Picho | last post by:
Hi all, I popped up this question a while ago, and I thought it was worth checking again now... (maybe something has changed or something will change). I read this book about component...
14
by: knocte | last post by:
Hello. I have a problem with C# language. I want to define an algorithm on a static function inside an abstract class. This function calls a static variable inside the same class. Then I want to...
1
by: Arnaud Debaene | last post by:
Hello, I think I found a bug in VC 7.1 concerning destruction of stack objects when linking a static, non managed, C++ library within a managed C++ application. Here is a repro case : 1)...
12
by: Hemanth | last post by:
Hi, I have a base class with a static constructor and some abstract methods. Derived classes implement these methods. From articles on the web, it appears that there is no guarentee that this...
1
by: Sandro Bosio | last post by:
Hello everybody, my first message on this forum. I tried to solve my issue by reading other similar posts, but I didn't succeed. And forgive me if this mail is so long. I'm trying to achieve the...
0
by: SimonDotException | last post by:
I've written an abstract base type which uses generics to provide XML serialization and deserialization methods for use by its derived types, but I'm seemingly unable to write it in a way which...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.