一章:概述(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
}
采用命令行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
}
相关文章推荐
- 一章:概述(Part 2)
- SharePoint服务器端对象模型 之 对象模型概述(Part 1)
- [AlwaysOn Availability Groups]健康模型 Part 1——概述
- atitit.atiOrmStoreService 框架的原理与设计 part1 概述与新特性
- 线程局部存储,Part 1:概述
- atitit.atiOrmStoreService 框架的原理与设计 part1 概述与新特性
- Windows Via C/C++ Part Ⅰ Chapter4: 进程(1)_概述
- ASP.NET - AppearanceEditorPart Web 服务器控件概述
- ASP.NET - BehaviorEditorPart Web 服务器控件概述
- SharePoint服务器端对象模型 之 对象模型概述(Part 2)
- ASP.NET - DeclarativeCatalogPart Web 服务器控件概述
- H.264/MPEG-4 Part 10 White Paper 翻译(1)--概述
- Part 1 ——ActiveMQ 概述
- ASP.NET - ImportCatalogPart Web 服务器控件概述
- Exchange Server 2013 LAB Part 1.Exchange 2013概述
- MySQL - 日期和时间类型概述
- 面向对象技术UML教程——第二章:UML概述(一)
- xcode调试技巧 part-1
- objective-c内存管理ARC关键字概述
- 【疯狂Java学习笔记】【第一章:Java语言概述】