始终处置实现IDisposible接口的对象? [英] Always dispose of an object that implements the IDisposible interface?
问题描述
如果一个对象实现了IDisposable接口(无论它是一个
框架对象还是一个用户对象),我应该总是原则上处理掉那个
对象吗?
If an object implements the IDisposable interface (regardless if its a
framework object or a user object), should I always dispose of that
object out of principle?
推荐答案
" Daniel Mori" < SB ****** @ hotmail.com>写道:
"Daniel Mori" <sb******@hotmail.com> wrote:
如果一个对象实现了IDisposable接口
(无论它是一个框架对象还是一个用户
对象),我是否应该总是处置该对象<原则上不合理?
If an object implements the IDisposable interface
(regardless if its a framework object or a user
object), should I always dispose of that object
out of principle?
如果一个类实现了IDisposable,它几乎肯定有资源需要释放的资源 - 所以是的,你应该。一个''使用''块将确保
,你不要忘记这样做:
使用(DisposableThing x = new DisposableThing())
{
// ...用x做点什么...
}
P.
If a class implements IDisposable, it almost certainly has resources
that need freeing up - so yes, you should. A ''using'' block will ensure
that you don''t forget to do this:
using (DisposableThing x = new DisposableThing())
{
// ... do something with x ...
}
P.
" Paul E Collins" <音响****************** @ CL4.org>在消息中写道
news:cl ********** @ hercules.btinternet.com ...
"Paul E Collins" <fi******************@CL4.org> wrote in message
news:cl**********@hercules.btinternet.com...
如果一个类实现了IDisposable,它几乎可以肯定有资源
需要释放 - 所以是的,你应该。一个''使用''块将确保你不要忘记这样做:
If a class implements IDisposable, it almost certainly has resources
that need freeing up - so yes, you should. A ''using'' block will ensure
that you don''t forget to do this:
我不知道这是否总是真正。例如,sqlConnection可以关闭
。
Michael Culley
I don''t know if this is always true. For example the sqlConnection can just
be closed.
Michael Culley
" Michael Culley" ; < MC ***** @ NOSPAMoptushome.com.au>在消息中写道
news:u3 ************** @ TK2MSFTNGP11.phx.gbl ...
"Michael Culley" <mc*****@NOSPAMoptushome.com.au> wrote in message
news:u3**************@TK2MSFTNGP11.phx.gbl...
" Paul E Collins" ; <音响****************** @ CL4.org>在消息中写道
新闻:cl ********** @ hercules.btinternet.com ...
"Paul E Collins" <fi******************@CL4.org> wrote in message
news:cl**********@hercules.btinternet.com...
如果一个类实现了IDisposable,它几乎肯定有资源
If a class implements IDisposable, it almost certainly has resources
that need freeing up - so yes, you should. A ''using'' block will ensure
that you don''t forget to do this:
我不知道这是否总是如此。例如,sqlConnection可以关闭。
I don''t know if this is always true. For example the sqlConnection can
just
be closed.
今天,SqlConnection的Dispose方法唯一能做的就是确保
连接已关闭。明天,它可以做其他事情。如果你想要成为一个好公民,请打电话给Dispose并假装你不知道这是什么。
Today, the only thing that SqlConnection''s Dispose method does is make sure
the connection is closed. Tomorrow, it could do something else. If you
want to be a good citizen, call Dispose and pretend that you don''t know what
it does.
这篇关于始终处置实现IDisposible接口的对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!