473,378 Members | 1,383 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,378 software developers and data experts.

Static constructor in derived class?

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 add items to this list, but have the base
class hold the data and expose the property.

Problem is, however, that the derived class' static constructor doesn't get
called when DerivedClass.MyList is accessed, so the list has no members. If
I move the MyList property up to DerivedClass, it works as expected and
DerivedClass' static constructor is invoked.

I can understand why the BaseClass static constructor is called, but it
would also seem to make sense that DerivedClass' static constructor would be
called when an inherited static member is accessed/invoked..

Thoughts?

Thanks,
Kirk

public class DerivedClass : BaseClass
{
static DerivedClass
{
_list.Add("Foo");
}

[...]
}

public class BaseClass
{
protected static List<string> _list = new List<string>();
public static List<string> MyList
{
return _list;
}

static BaseClass()
{
}

[...]
}

Nov 16 '05 #1
3 11795
The static constructor is just another method. So why would calling one
static method (MyList) cause another static method (derived class ctor) to
be invoked? There is no logical connection between those two. If you want to
add items to your list, you'll have to create a method to do it. (A static
base class method.)

--
kevin aubuchon
www.aubuchon-design.com
"Kirk Marple" <Kirk Ma****@discussions.microsoft.com> wrote in message
news:A9**********************************@microsof t.com...
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 add items to this list, but have the base
class hold the data and expose the property.

Problem is, however, that the derived class' static constructor doesn't get called when DerivedClass.MyList is accessed, so the list has no members. If I move the MyList property up to DerivedClass, it works as expected and
DerivedClass' static constructor is invoked.

I can understand why the BaseClass static constructor is called, but it
would also seem to make sense that DerivedClass' static constructor would be called when an inherited static member is accessed/invoked..

Thoughts?

Thanks,
Kirk

public class DerivedClass : BaseClass
{
static DerivedClass
{
_list.Add("Foo");
}

[...]
}

public class BaseClass
{
protected static List<string> _list = new List<string>();
public static List<string> MyList
{
return _list;
}

static BaseClass()
{
}

[...]
}

Nov 16 '05 #2
From the CLI guidelines:

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

If BaseClass is an abstract class, then it might make sense that the derived
class' constructor would be invoked when any of the static members of either
class (derived or base) are referenced.

Basically, I wasn't sure if the inheritance chain affected the invocation
semantics of static constructors.

Kirk

"Kevin Aubuchon" wrote:
The static constructor is just another method. So why would calling one
static method (MyList) cause another static method (derived class ctor) to
be invoked? There is no logical connection between those two. If you want to
add items to your list, you'll have to create a method to do it. (A static
base class method.)

--
kevin aubuchon
www.aubuchon-design.com
"Kirk Marple" <Kirk Ma****@discussions.microsoft.com> wrote in message
news:A9**********************************@microsof t.com...
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 add items to this list, but have the base
class hold the data and expose the property.

Problem is, however, that the derived class' static constructor doesn't

get
called when DerivedClass.MyList is accessed, so the list has no members.

If
I move the MyList property up to DerivedClass, it works as expected and
DerivedClass' static constructor is invoked.

I can understand why the BaseClass static constructor is called, but it
would also seem to make sense that DerivedClass' static constructor would

be
called when an inherited static member is accessed/invoked..

Thoughts?

Thanks,
Kirk

public class DerivedClass : BaseClass
{
static DerivedClass
{
_list.Add("Foo");
}

[...]
}

public class BaseClass
{
protected static List<string> _list = new List<string>();
public static List<string> MyList
{
return _list;
}

static BaseClass()
{
}

[...]
}


Nov 16 '05 #3
Sounds correct. Technically static members are not inherited at all,
but C# and the IDE provide the illusion of inheritance. When you call
DerivedClass.BaseStaticMember the compiler will generate IL for
BaseClass.BaseStaticMember.

--
Scott
http://www.OdeToCode.com/blogs/scott/

On Sun, 28 Nov 2004 17:49:07 -0800, "Kirk Marple" <Kirk
Ma****@discussions.microsoft.com> wrote:
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 add items to this list, but have the base
class hold the data and expose the property.

Problem is, however, that the derived class' static constructor doesn't get
called when DerivedClass.MyList is accessed, so the list has no members. If
I move the MyList property up to DerivedClass, it works as expected and
DerivedClass' static constructor is invoked.

I can understand why the BaseClass static constructor is called, but it
would also seem to make sense that DerivedClass' static constructor would be
called when an inherited static member is accessed/invoked..

Thoughts?

Thanks,
Kirk

public class DerivedClass : BaseClass
{
static DerivedClass
{
_list.Add("Foo");
}

[...]
}

public class BaseClass
{
protected static List<string> _list = new List<string>();
public static List<string> MyList
{
return _list;
}

static BaseClass()
{
}

[...]
}


Nov 16 '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: Elia Karagiannis | last post by:
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...
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...
5
by: none | last post by:
I'd like to create a new static property in a class "hiding" the property present in a base class. Since this needs to happen at runtime I tried doing this via DynamicMethod. But obviously the...
8
by: Per Bull Holmen | last post by:
Hey Im new to c++, so bear with me. I'm used to other OO languages, where it is possible to have class-level initialization functions, that initialize the CLASS rather than an instance of it....
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...
4
by: softwaredoug | last post by:
Here is some test code I've been attempting to compile (Visual Studio 2003) test.h: class Base { protected: Base() {} public:
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...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...

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.