2005-10-27

Shared Or Not?

Given these classes, which static members are shared?

class Base { public static int m_b; }
class Sub : Base { }
class GenA<T> : Base { public static int m_g; }
class SubGenA : GenA<int> { public static int m_sg; }
class SubSubGenA : SubGenA { }

Shared:

Base.m_b / Base.m_b
Base.m_b / Sub.m_b
Base.m_b / GenA<int>.m_b
GenA<int>.m_g / GenA<int>.m_g
GenA<int>.m_g / SubGenA.m_g
SubGenA.m_sg / SubSubGenA.m_sg

Not shared:

GenA<int>.m_g / GenA<string>.m_g
GenA<string>.m_g / SubGenA.m_g

I first tried for a truth table, but it got so big and hairy that I decided to simplify.

Why is this relevant? Because I need to share connections and transactions between classes in a controlled manner. That means sharing where I need to share and not sharing at other times. These class semantics determine what the language will do for me and what I must handle myself (and where).

These examples do not include the use of the [ThreadStatic] attribute. Presumably, the rules implied by the examples above would apply the same when marked with the [ThreadStatic] attribute, but only within a given thread. Members in other threads will have their own instance.