【发布时间】:2010-08-10 21:09:00
【问题描述】:
我最近正在阅读一篇垃圾收集文章,并决定继续尝试并尝试获得更深入的了解。我编写了以下代码,玩弄了using 语句,但对结果感到惊讶......我希望在 using 块之外的 e.Parent.Name 会去 ka-blooey。
这里到底发生了什么?
static void Main(string[] args)
{
Employee e = new Employee();
using (Parent p = new Parent())
{
p.Name = "Betsy";
e.Parent = p;
Console.WriteLine(e.Parent.Name);
}
Console.WriteLine(e.Parent.Name);
Console.ReadLine();
}
public class Employee
{
public Parent Parent;
}
public class Parent : IDisposable
{
public string Name;
public void Dispose()
{
Console.WriteLine("Disposing Parent");
}
}
【问题讨论】:
-
您可能对 Raymond Chen 最近的博客文章Everybody Thinks About Garbage Collection the Wrong Way 感兴趣。
标签: c# memory-management