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

Static constructor in derived class?

P: n/a
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
Share this Question
Share on Google+
3 Replies


P: n/a
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

P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.