您的位置:首页 > 其它

一章:概述(Part 1)

2011-07-04 13:44 148 查看
c#是一种区分大小写的语言。

采用命令行CSC.EXE 编译程序。

IDisposable模式

C#中的昂贵资源比如打开文件的句柄,连接数据库等,都需要通过手工调用IDisposable.Dispose()去释放。并且using语句可以帮助用户及时调用Dispose方法。

等于在try..catch块中finally中调用该方法。

public class Demo : IDisposable
{

private bool disposed = false;
#region IDisposable Members

public void Dispose()
{
//the value is true means the dispose method is called by user directly.
Dispose(true);

//This object will be cleaned up by the Dispose method. Therefore, you should call GC.SupressFinalize to take this object
//off the finalization queue and prevent finalization code for this object from executing a second time.
GC.SuppressFinalize(this);
}

private void Dispose(bool disposing)
{
if (disposed)
{
return;
}
if (disposing)
{
//release managed resources.

}
//release unmanaged resources

disposed = true;
}

~Demo()
{
Dispose(false);
}
#endregion
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: