• C# 析构函数


    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(); } } }
  • 相关阅读:
    正则表达式语法介绍
    关系型数据库和非关系型数据库的简单对比
    lambda函数
    java基础系列--Exception异常处理
    springBoot基础系列--properties配置
    spring基础系列--JavaConfig配置
    java基础系列--Calendar类
    java基础系列--Date类
    一个特殊的List去重问题的解决方案
    Java学习笔记
  • 原文地址:https://www.cnblogs.com/fat_li/p/1828000.html
Copyright © 2020-2023  润新知