close()和dispose() [英] close() and dispose()

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

问题描述

一旦我完成了一个物体,这两个都不是一个坏主意吗?


Eric B.

解决方案

我总是关闭&处理。


它可能会清除内存分配。但如果你在一个

私有方法中,一旦你离开那个

方法它就可能会被破坏。

10月9日, 12:32 * am,Eric B. < bigb ... @ sesamestreet.comwrote:


一旦我完成了一个对象,这两个都不是一个坏主意吗?


Eric B.


2008年10月9日星期四00:32:09 -0700,Eric B.< bi * ****@sesamestreet.com>

写道:


一旦完成,两者都是一个坏主意一个东西?



这两种做法从来都不是_bad_想法。


许多类都证明这两种方法是等价的,或者那个

Close()总是调用Dispose(),就是那种东西。而且我想在那些情况下,你可以合法地逍遥法外只做一个或者另外一个(根据文档的话)。


但就个人而言,我倾向于两者都做。对我来说,两者在语义上是不同的,即使他们经常在实践中做同样的事情。


Pete


10月9日上午9:00 *,Slickuser< slick.us ... @ gmail.comwrote:


我总是做*关闭&处理。


它可能会清除内存分配。



不,它不会。


但如果你在里面

私有方法,一旦你离开

方法,它可能会被破坏。



不,它不会。


关闭()和Dispose()不释放内存。他们偶尔可能会发布他们可能对其他对象提供的引用,但除非他们明确调用GC.Collect() - 这将是一个坏主意 -

'不会释放任何内存。


我个人只是使​​用使用块会自动调用

Dispose(),而不用担心Close()。如果我遇到过一个类型

,它实现了IDisposable但我需要明确关闭()在

常见情况下,我会向作者抱怨。 />

Jon


Is it a bad idea to do both once I''m done with an object?

Eric B.

解决方案

I always do close & dispose.

It might clear up the memory allocation. But if you''re inside a
private method, it will probably get destroy once you leave that
method.
On Oct 9, 12:32*am, "Eric B." <bigb...@sesamestreet.comwrote:

Is it a bad idea to do both once I''m done with an object?

Eric B.


On Thu, 09 Oct 2008 00:32:09 -0700, Eric B. <bi*****@sesamestreet.com>
wrote:

Is it a bad idea to do both once I''m done with an object?

It''s never a _bad_ idea to do both.

Many classes document that the two methods are equivalent, or that the
Close() always calls Dispose(), that sort of thing. And I suppose in
those cases, you can legitimately get away with doing only one or the
other (as appropriate based on the docs).

But personally, I tend to always do both. To me, the two are semantically
different, even if they often wind up doing the same thing in practice.

Pete


On Oct 9, 9:00*am, Slickuser <slick.us...@gmail.comwrote:

I always do * close & dispose.

It might clear up the memory allocation.

No it won''t.

But if you''re inside a
private method, it will probably get destroy once you leave that
method.

No it won''t.

Close() and Dispose() don''t release memory. They may occasionally
release references that they may have to other objects, but unless
they then explicitly call GC.Collect() - which would be a bad idea -
that''s not going to release any memory.

Personally I just use a "using" block which will automatically call
Dispose(), and don''t worry about Close(). If I ever come across a type
which implements IDisposable but which I need to explicitly Close() in
common situations, I''ll complain to the author.

Jon


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

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