Code Inspection: Static member in generic type
In the vast majority of cases, having a static field or auto-property in a generic type is a sign of an error. The reason for this is that a static member in a generic type will not be shared among instances of different close constructed types. This means that for a generic class MyGeneric<T>
which has public static string MyProp { get; set; }
, the values of MyGeneric<int>.MyProp
and MyGeneric<string>.MyProp
have completely different, independent values.
If you need to have a static field shared between instances with different generic arguments, define a non-generic base class to store your static members, then set your generic type to inherit from this type.
Last modified: 07 April 2022