泛型类的静态成员是否绑定到特定实例?

这更像是一个文档,而不是一个真正的问题。这似乎还没有被提到(除非我错过了) ,所以这里是:

设想一个包含静态成员的泛型类:

class Foo<T> {
public static int member;
}

是每个特定类都有一个新的成员实例,还是所有 Foo 类型的类都只有一个实例?

它可以很容易地通过以下代码进行验证:

Foo<int>.member = 1;
Foo<string>.member = 2;
Console.WriteLine (Foo<int>.member);

结果是什么? 这种行为记录在哪里?

20569 次浏览

They are not shared. Not sure where it's documented but analysis warning CA1000 (Do not declare static members on generic types) warns against just this due to the risk of making the code more complicated.

IMO, you need to test it, but I think that

Foo<int>.member = 1;
Foo<string>.member = 2;
Console.WriteLine (Foo<int>.member);

will output 1 because I think that, during compilation, the compilator create 1 class for every generic class you use (in you example : Foo<int> and Foo<string>).

But I'm not 100% sure =).

Remark : I think it's not a good design nor a good practice to use such kind of static attributes.

A static field is shared across all instances of the same type. Foo<int> and Foo<string> are two different types. This can be proven by the following line of code:

// this prints "False"
Console.WriteLine(typeof(Foo<int>) == typeof(Foo<string>));

As for where this is documented, the following is found in section 1.6.5 Fields of the C# Language Specification (for C# 3):

A static field identifies exactly one storage location. No matter how many instances of a class are created, there is only ever one copy of a static field.

As stated before; Foo<int> and Foo<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:

A generic type declaration, by itself, denotes an unbound generic type that is used as a “blueprint” to form many different types, by way of applying type arguments.

They are not really shared. Because the member doesn't belong to the instance at all. A static class member belongs to the class itself. So, if you have MyClass.Number it is the same for all MyClass.Number objects because it not even depends on the object. You can even call or modify MyClass.Number without any object.

But since Foo< int > is not the same class as Foo< string > these two numbers are not shared.

An example to show this:

TestClass<string>.Number = 5;
TestClass<int>.Number = 3;


Console.WriteLine(TestClass<string>.Number);  //prints 5
Console.WriteLine(TestClass<int>.Number);     //prints 3

The problem here is actually the fact that "generic classes" are not classes at all.

Generic class definitions are just templates for classes, and until their type parameters are specified, they are just a piece of text (or a handful of bytes).

At runtime, one can specify a type parameter for the template, thus bringing it to life, and creating a class of the, now, fully specified type. That's why static properties are not template-wide, and that's why you cannot cast between List<string> and List<int>.

That relationship kinda mirrors the class-object relationship. Just like classes do not exist* until you instantiate an object from them, generic classes do not exist, until you make a class based on the template.

P.S. It's quite possible to declare

class Foo<T> {
public static T Member;
}

From this is kinda obvious that the static members cannot be shared, as T is different for different specializations.

C# implementation of generics is more closer to C++. In both of these languages MyClass<Foo> and MyClass<Bar> don't share static members but in Java they do. In C# and C++ MyClass<Foo> internally creates entirely new type at compile time as if generics are kind of macros. You can usually see their generated names in stack trace, like MyClass'1 and MyClass'2. This is why they don't share static variables. In Java, generics are implemented by more simpler method of compiler generating code using non-generic types and adding type casts all over. So MyClass<Foo> and MyClass<Bar> don't generate two entirely new class in Java, instead they both are same class MyClass underneath and that's why they share static variables.