c# 中使用构构函数与IDisposable接口双重释放资源

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. namespace IDisposeDemo
  5. {
  6.     class 回收资源示例
  7.     {
  8.         public static void Main( string[] args )
  9.         {
  10.             using ( Person p = new Student() )
  11.             {
  12.             }
  13.             
  14.             //Person p = new Person();
  15.             //p = null;
  16.             //Console.Read();
  17.             Console.ReadLine();
  18.         }
  19.     }
  20.     class EmailInfo : IDisposable
  21.     {
  22.         public string address;
  23.         public bool isVip;
  24.         public EmailInfo( string address , bool isVip )
  25.         {
  26.             this.address = address;
  27.             this.isVip = isVip;
  28.         }
  29.         #region IDisposable 成员
  30.         public void Dispose()
  31.         {
  32.             this.address = null;
  33.             Console.WriteLine( "Email中的成员被释放");
  34.         }
  35.         #endregion
  36.     }
  37.     class Person : IDisposable
  38.     {
  39.         protected bool _flag = true;
  40.         EmailInfo email = new EmailInfo( "aladdin" , true );
  41.         
  42.         //析构
  43.         ~Person()
  44.         {
  45.             Console.WriteLine( "调用析构");
  46.             this.Dispose( false );
  47.         }
  48.         //虚方法
  49.         protected virtual void Dispose( bool isTrusteeship )
  50.         {
  51.             //是否执行过回收
  52.             if ( this._flag )
  53.             {
  54.                 if ( isTrusteeship )
  55.                 {
  56.                     //释放托管与非托管资源
  57.                     this.email.Dispose();
  58.                     Console.WriteLine( "数据库与文件流等非拖被Dispose()方法释放" );
  59.                 }
  60.                 else
  61.                 {
  62.                     //释放非托管资源
  63.                     Console.WriteLine( "数据库与文件流等非拖被析构方法释放" );
  64.                 }
  65.             }
  66.             this._flag = false;
  67.         }
  68.         #region IDisposable 成员
  69.         public void Dispose()
  70.         {
  71.             this.Dispose( true );
  72.             //标记本对象回后时不要再调用析构
  73.             GC.SuppressFinalize( this );
  74.         }
  75.         #endregion
  76.     }
  77.     class Student : Person
  78.     {
  79.         public string stuNum;
  80.         ~Student()
  81.         {
  82.             this.Dispose( false );
  83.         }
  84.         protected override void Dispose( bool isTrusteeship )
  85.         {
  86.             //是否执行过回收
  87.             if ( this._flag )
  88.             {
  89.                 if ( isTrusteeship )
  90.                 {
  91.                     //释放托管与非托管资源
  92.                     //释放学生自已的成员引用
  93.                     Console.WriteLine( "学员的资源被释放" );
  94.                 }
  95.                 else
  96.                 {
  97.                     //释放非托管资源
  98.                     Console.WriteLine( "学员的非托管资源被析构回收" );
  99.                 }
  100.             }
  101.             base.Dispose( isTrusteeship );
  102.         }
  103.     }
  104. }
  105. /**
  106. 在以上示例中,设计到子类与父类的资源回收, 子类Student在回收时,显示调用父类的方法,这样可以做到层层回收
  107.  * student回收完成后,接着是父类person的实例成员回收(Email)
  108.  * _flag标记最后由父类进行更新,这样既例调用多次回收,也没关系
  109. */
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值