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);

运行结果:

注销了
空
posted @ 2023-11-27 16:56  Simian_2018_12_22  阅读(4)  评论(0编辑  收藏  举报