Static constructor in derived class?

G

Guest

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()
{
}

[...]
}
 
K

Kevin Aubuchon

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

Guest

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 said:
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 said:
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()
{
}

[...]
}
 
S

Scott Allen

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top