垃圾收集器和处置 [英] Garbage Collector and dispose

查看:65
本文介绍了垃圾收集器和处置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,


只是一个简短的问题:如果一个类实现了IDisposable接口,那么垃圾收集器总是调用Dispose()方法这个班级,在

之前它被清理干净了吗?


问候

Rainer

Hi NG,

just a short question: If a class implements the IDisposable interface, does
the garbage collector always call the Dispose() method of the class, before
it is cleaned up?

Regards
Rainer

推荐答案



" Rainer Queck" < Ra **** @ noemail.noemailwrote in message

news:Ol ************** @ TK2MSFTNGP03.phx.gbl ...

"Rainer Queck" <Ra****@noemail.noemailwrote in message
news:Ol**************@TK2MSFTNGP03.phx.gbl...

你好,


只是一个简短的问题:如果一个类实现了IDisposable接口,

垃圾收集器总是在清理之前调用类的Dispose()方法,




问候

Rainer
Hi NG,

just a short question: If a class implements the IDisposable interface,
does the garbage collector always call the Dispose() method of the class,
before it is cleaned up?

Regards
Rainer



阅读此处的备注部分:

http://msdn2.microsoft.com/en-us/lib...e.dispose.aspx


< quote>

因为必须显式调用Dispose方法,所以实现

IDisposable的对象还必须实现一个终结器来处理释放资源时

不调用Dispose。

< / quote>

Read the remarks section here:

http://msdn2.microsoft.com/en-us/lib...e.dispose.aspx

<quote>
Because the Dispose method must be called explicitly, objects that implement
IDisposable must also implement a finalizer to handle freeing resources when
Dispose is not called.
</quote>





你应该c所有这一切,无论是通过直接调用它还是使用使用

构造:


使用(SqlConnection con = new SqlConnection ...)

{

} //在这里处理

但是回答你原来的问题,答案是肯定的,它确实称之为

最终确定对象(这不是确定性的)


看一下实现一个Dispose方法在MSDN中


另请注意,您只需要在使用非托管资源时实施它

资源


Rainer Queck < Ra **** @ noemail.noemailwrote in message

news:Ol ************** @ TK2MSFTNGP03.phx.gbl ...
Hi,

You should call it, either by direcly calling it or by using the using
construction:

using(SqlConnection con = new SqlConnection ...)
{
} //Disposed here
But answering your original question, the answer is yes, it does call it
when finalizing the object (which is not deterministic)

Take a look at the "Implementing a Dispose method" in MSDN

Also note that you only need to implement it when you are using non-managed
resources

"Rainer Queck" <Ra****@noemail.noemailwrote in message
news:Ol**************@TK2MSFTNGP03.phx.gbl...

你好,


只是一个简短的问题:如果一个类实现了IDisposable接口,

垃圾收集器总是在清理之前调用类的Dispose()方法,




问候

Rainer
Hi NG,

just a short question: If a class implements the IDisposable interface,
does the garbage collector always call the Dispose() method of the class,
before it is cleaned up?

Regards
Rainer



感谢您的提示!


问候

Rainer
thanks for this hint!

Regards
Rainer


这篇关于垃圾收集器和处置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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