c# 中使用构构函数与IDisposable接口双重释放资源
2008-11-16 12:13
351 查看
using System;
using System.Collections.Generic;
using System.Text;
namespace IDisposeDemo
{
class 回收资源示例
{
public static void Main( string[] args )
{
using ( Person p = new Student() )
{
}
//Person p = new Person();
//p = null;
//Console.Read();
Console.ReadLine();
}
}
class EmailInfo : IDisposable
{
public string address;
public bool isVip;
public EmailInfo( string address , bool isVip )
{
this.address = address;
this.isVip = isVip;
}
#region IDisposable 成员
public void Dispose()
{
this.address = null;
Console.WriteLine( "Email中的成员被释放");
}
#endregion
}
class Person : IDisposable
{
protected bool _flag = true;
EmailInfo email = new EmailInfo( "aladdin" , true );
//析构
~Person()
{
Console.WriteLine( "调用析构");
this.Dispose( false );
}
//虚方法
protected virtual void Dispose( bool isTrusteeship )
{
//是否执行过回收
if ( this._flag )
{
if ( isTrusteeship )
{
//释放托管与非托管资源
this.email.Dispose();
Console.WriteLine( "数据库与文件流等非拖被Dispose()方法释放" );
}
else
{
//释放非托管资源
Console.WriteLine( "数据库与文件流等非拖被析构方法释放" );
}
}
this._flag = false;
}
#region IDisposable 成员
public void Dispose()
{
this.Dispose( true );
//标记本对象回后时不要再调用析构
GC.SuppressFinalize( this );
}
#endregion
}
class Student : Person
{
public string stuNum;
~Student()
{
this.Dispose( false );
}
protected override void Dispose( bool isTrusteeship )
{
//是否执行过回收
if ( this._flag )
{
if ( isTrusteeship )
{
//释放托管与非托管资源
//释放学生自已的成员引用
Console.WriteLine( "学员的资源被释放" );
}
else
{
//释放非托管资源
Console.WriteLine( "学员的非托管资源被析构回收" );
}
}
base.Dispose( isTrusteeship );
}
}
}
/**
在以上示例中,设计到子类与父类的资源回收, 子类Student在回收时,显示调用父类的方法,这样可以做到层层回收
* student回收完成后,接着是父类person的实例成员回收(Email)
* _flag标记最后由父类进行更新,这样既例调用多次回收,也没关系
*/
using System.Collections.Generic;
using System.Text;
namespace IDisposeDemo
{
class 回收资源示例
{
public static void Main( string[] args )
{
using ( Person p = new Student() )
{
}
//Person p = new Person();
//p = null;
//Console.Read();
Console.ReadLine();
}
}
class EmailInfo : IDisposable
{
public string address;
public bool isVip;
public EmailInfo( string address , bool isVip )
{
this.address = address;
this.isVip = isVip;
}
#region IDisposable 成员
public void Dispose()
{
this.address = null;
Console.WriteLine( "Email中的成员被释放");
}
#endregion
}
class Person : IDisposable
{
protected bool _flag = true;
EmailInfo email = new EmailInfo( "aladdin" , true );
//析构
~Person()
{
Console.WriteLine( "调用析构");
this.Dispose( false );
}
//虚方法
protected virtual void Dispose( bool isTrusteeship )
{
//是否执行过回收
if ( this._flag )
{
if ( isTrusteeship )
{
//释放托管与非托管资源
this.email.Dispose();
Console.WriteLine( "数据库与文件流等非拖被Dispose()方法释放" );
}
else
{
//释放非托管资源
Console.WriteLine( "数据库与文件流等非拖被析构方法释放" );
}
}
this._flag = false;
}
#region IDisposable 成员
public void Dispose()
{
this.Dispose( true );
//标记本对象回后时不要再调用析构
GC.SuppressFinalize( this );
}
#endregion
}
class Student : Person
{
public string stuNum;
~Student()
{
this.Dispose( false );
}
protected override void Dispose( bool isTrusteeship )
{
//是否执行过回收
if ( this._flag )
{
if ( isTrusteeship )
{
//释放托管与非托管资源
//释放学生自已的成员引用
Console.WriteLine( "学员的资源被释放" );
}
else
{
//释放非托管资源
Console.WriteLine( "学员的非托管资源被析构回收" );
}
}
base.Dispose( isTrusteeship );
}
}
}
/**
在以上示例中,设计到子类与父类的资源回收, 子类Student在回收时,显示调用父类的方法,这样可以做到层层回收
* student回收完成后,接着是父类person的实例成员回收(Email)
* _flag标记最后由父类进行更新,这样既例调用多次回收,也没关系
*/
相关文章推荐
- C#中using以及实现接口IDisposable的使用方法
- C#资源回收和IDisposable接口的使用
- IDisposable接口和析构函数的联合使用
- C#的抽象类和接口的区别,在什么时候使用才合适?
- c#.net]做tooltip给控件动态添加属性的做法、IExtenderProvider接口的使用
- C#控制台基础 快速实现接口的中的所有成员,使用快捷键
- C#------如何使用Swagger调试接口
- C#中接口和抽象类的使用上的主要差别在哪里?
- 何时使用委托而不使用接口 (C#)
- C#中抽象类和接口的区别与使用
- C#如何使用REST接口读写数据
- 转 C#开发微信门户及应用(1)--开始使用微信接口
- 黄聪:C#尽量使用接口来编程
- C#开发微信门户及应用(42)--使用Autofac实现微信接口处理的控制反转处理
- C#如何使用REST接口读写数据
- 何时使用委托而不使用接口(C# 编程指南)
- java调用C# webservice接口使用axis报错
- .NET(C#):使用UPnP来穿透NAT使内网接口对外网可见
- C# 接口和抽象类的区别和使用场合(知识要点归纳总结)
- C#开发微信门户及应用(1) 微信接口使用