了解破坏和基类 [英] Understanding destruction and base class

查看:93
本文介绍了了解破坏和基类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以帮我清理一下吗?我不太了解如何正确处理基类中的非托管资源.例如,假设我有一个基类,我想与数据库建立基本连接.像

Can someone please clear up something for me? I don''t quite understand how to properly handle an un-managed resource from a base class. For example, say I have a base class that I want to do basic connectivity to a database. Something like

public abstract class MyBaseClass
{
  SQLiteConnection MyConnection {get;set;}
  public MyBaseClass(string connString)
{
 this.MyConnection = new SQLiteConnection(connString);
this.MyConnection.Open();
}
  ~MyBaseClass()
{
  if (this.DBConn != null)
{
    this.DBConn.Close();
    this.DBConn.Dispose();
}
}



还有一个继承类:



And a the inheriting class:

public class MyClass:MyBaseClass
{
 public MyClass(string ConnString):base(string ConnString)
{ 
 
}

~MyClass()
{
//what do I need to do here?
}



我的理解是,当MyClass的实例超出范围时,GC会在某个时候处置它.完成后,它将调用〜MyClass()析构函数. (正确吗?)因此,在清理MyClass时,它将自动调用基本析构函数吗?由于GC.Collect是不确定的,因此这很难测试,在我的情况下,它位于生命周期不确定的线程上.

我计划从无法预测其寿命的线程中调用这些类型,并且可能同时存在多个实例.在这种情况下,我以前是使用IDisposable接口实现的,但是如果我做对的话,上面的内容似乎要容易得多.我想我只是不清楚partent和基类中的〜destructors都将使用GC.

像往常一样感谢.



My understanding is that when an instance of MyClass goes out of scope, GC will at some point dispose of it. When it does it will call the ~MyClass() destructor. (Correct?) So, when MyClass is getting cleaned up, will it automatically call the base destructor? This is a little hard to test since the GC.Collect is indeterminate, and in my case it is on a thread whose lifetime is indeterminate.

I am planning on calling these types from a thread where its lifetime is not predictable, and there will likely be multiple instances at the same time. In this circumstance I had previously implemented with the IDisposable interface, but the above seems much easier if I''m doing it right. I guess I''m just not clear what GC is going to do with the ~destructors both in the partent and base class.

Thanks as always.

推荐答案

.NET中的首选方法是使用Dispose pattern清理非托管资源,因为不确定何时在Finalize 方法由GC调用.请仔细阅读以下文章

正确实施IDisposable和处置模式 [ ^ ]
http://msdn.microsoft.com/en-us/library/fs2xkftw.aspx [ ^ ]
The preferred approach in .NET is to use Dispose pattern for clean up of unmanaged resources, as it is not certain at what time Finalize method is called by the GC. Please go through the following articles

Implementing IDisposable and the Dispose Pattern Properly[^]
http://msdn.microsoft.com/en-us/library/fs2xkftw.aspx[^]


资源分配的首选方法是Dispose方法,而不是析构函数.使用完资源后,仅在类需要释放某些资源时才需要这样做,然后在使用完正在使用资源的类时,调用Dispose方法.这意味着,如果有一个类使用的资源,则应包括一个Dispose方法.这样做的原因是因为显式使用Displose方法(并在完成类后调用它)意味着内存管理不必处理,可以更早地释放资源.同样实现一个dispose方法意味着可以使用Using语句,一旦using语句范围内的代码退出该范围,它将自动取消资源分配.通常〜方法中包含.Net代码,实际上将调用Dispose方法以覆盖程序员未调用dispose方法的基础.
The perfered method for the disposal of resources is the Dispose method, not the destructor. When finished using a resource, and this is only required if a class needs to dispose of some resource, then call the Dispose method when finished using the class that is using the resource. That means that if there is a resource that a class uses, then should include a Dispose method. The reason for this is because explicitly using a Displose method (and calling it when finished with a class) means the Memory Management does not have to deal with, releasing the resources earlier. Also implementing a dispose method means that the Using statement can be used, which will automatically Displose of the resource once the code within the scope of the using statement exits this scope. Generally with .Net code within the ~method will actually call the Dispose method to cover the base that the programmer did not call the dispose method.


这篇关于了解破坏和基类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆