1

Arguments aside about the sanity of doing this, I'm trying to confirm whether my understanding of the behavior of a static field in this scenario is correct.

If we have the following example code:

public class GenericBaseClass<T>
{
    public static object example = new object();
}

public class InheritedClassA : GenericBaseClass<string>
{

}

public class InheritedClassB : GenericBaseClass<int>
{

}

Am I correct in thinking that inheriting from a generic class in this manner will result in InheritedClassA and InheritedClassB having their own separate distinct instances of the static field named example (i.e. InheritedClassA.example and InheritedClassB.example) so that any change to, or use of example for locking in InheritedClassA will have no impact on the static field under InheritedClassB due to the fact that the compiler will effectively create two new concrete types from the generic base class with the defined type?

Does this then therefore cease to be true if InheritedClassA and InheritedClassB were to instead specify the same type for T?

Xefan
  • 645
  • 1
  • 12
  • 26

0 Answers0