This is more of a documentation than a real question. This does not seem to have been addressed on SO yet (unless I missed it), so here goes:
Imagine a generic class that contains a static member:
class Foo<T> {
public static int member;
}
Is there a new instance of the member for each specific class, or is there only a single instance for all Foo-type classes?
It can easily be verified by code like this:
Foo<int>.member = 1;
Foo<string>.member = 2;
Console.WriteLine (Foo<int>.member);
What is the result, and where is this behavior documented?
A
staticfield is shared across all instances of the same type.Foo<int>andFoo<string>are two different types. This can be proven by the following line of code:As for where this is documented, the following is found in section 1.6.5 Fields of the C# Language Specification (for C# 3):
As stated before;
Foo<int>andFoo<string>are not the same class; they are two different classes constructed from the same generic class. How this happens is outlined in section 4.4 of the above mentioned document: