【发布时间】:2011-09-29 19:34:42
【问题描述】:
静态成员变量会被垃圾回收吗?
例如,让我们使用下面的类。
public class HasStatic {
private static List<string> shared = new List<string>();
}
假设它是这样使用的:
//Startup
{
HasStatic a = new HasStatic();
HasStatic b = new HasStatic();
HasStatic c = new HasStatic();
HasStatic d = new HasStatic();
//Something
}
//Other code
//Things deep GC somewhere in here
HasStatic e = new HasStatic();
当a、b、c 和d 被垃圾回收时,静态成员shared 是否也会被回收? e 能否获得shared 的新实例?
【问题讨论】:
-
请记住
a.shared[1]与b.shared[1]具有相同的值,并且HasStatic的这个元素也可以通过HasStatic.shared[1]引用。 (当然,假设shared列表中至少有 2 个元素。)
标签: c# garbage-collection static-members