Child pages
  • Static field in generic type
Skip to end of metadata
Go to start of metadata

Redirection Notice

Icon

In the vast majority of cases, having a static field in a generic type is a sign of an error. The reason for this is that a static field in a generic type will not be shared among instances of different close constructed types. This means that for a generic class C<T> which has a static field X, the values of C<int>.X and C<string>.X have completely different, independent values.

In the rare cases when you do need the 'specialized' static fields, feel free to suppress the warning.

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.

  • No labels