IDisposable接口
实现了IDisposable接口的类会在离开using时自动调用Dispose方法
public class DisposeClass : IDisposable
{
public string Name { get; set; }
public void Dispose()
{
this.Name = "空";
Console.WriteLine("注销了");
}
}
使用代码:
DisposeClass d = null;
using(d = new DisposeClass()){
d.Name = "zzz";
}
Console.WriteLine(d.Name);
运行结果:
注销了
空