As pointed out in other answer, the base class static field will be shared between all the subclasses. If you need a separate copy for each final subclass, you can use a static dictionary with a subclass name as a key:
class Base
{
private static Dictionary<string, int> myStaticFieldDict = new Dictionary<string, int>();
public int MyStaticField
{
get
{
return myStaticFieldDict.ContainsKey(this.GetType().Name)
? myStaticFieldDict[this.GetType().Name]
: default(int);
}
set
{
myStaticFieldDict[this.GetType().Name] = value;
}
}
void MyMethod()
{
MyStaticField = 42;
}
}
abstract
is a red herring; it doesn't matter if the base class isabstract
or not, the behavior @Marc Gravell points out is the same. – HillyTypeLocal<T>
as we haveThreadLocal<T>
so any object of that type would bestatic
in it's correspondingsubclass
. – Totalizator