文章详情

  • 游戏榜单
  • 软件榜单
关闭导航
热搜榜
热门下载
热门标签
php爱好者> php文档>C# 析构函数

C# 析构函数

时间:2010-09-16  来源:胖子黎

using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ConsoleApplication1 { //public class MyTest : IDisposable //{ // #region IDisposable 成员 // public MyTest() // { // } // public void Dispose() // { // Console.WriteLine("IDisposable中"); // } // #endregion //} // class Program // { // static void Main(string[] args) // { // MyTest temp = null; // try // { // temp = new MyTest(); // Console.WriteLine("Try中"); // //do your processing; // } // finally // { // if (temp != null) temp.Dispose(); // } // //using (MyTest temp = new MyTest()) ; //这句可以替掉Try. // } // } public class ResourceHolder : IDisposable { private bool isDispose = false; #region IDisposable 成员 public void Dispose() { Dispose(true); GC.SuppressFinalize(this); } #endregion protected virtual void Dispose(bool disposing) //派生类才可以访问 { if (!isDispose) { if (disposing) { //cleanup managed object by calling the their Disposing method; } //cleanup unmanaged objects; } isDispose = true; } ~ResourceHolder() // { Dispose(false); } public void SomeMethod() { //Ensure object not already disposed before exception of any method if (isDispose) { throw new ObjectDisposedException("ResourceHolder"); } //method implementation... } } public class MainRun { static void Main() { ResourceHolder temp = new ResourceHolder(); } } }
相关阅读 更多 +
排行榜 更多 +
辰域智控app

辰域智控app

系统工具 下载
网医联盟app

网医联盟app

运动健身 下载
汇丰汇选App

汇丰汇选App

金融理财 下载