处理并关闭澄清 [英] Dispose and Close Clarification

查看:52
本文介绍了处理并关闭澄清的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hello Codeproject



任何人都可以向我解释为什么



使用Close方法或者Dispose方法就足够了。您不必一个接一个地调用一个方法。 在另一个方法之后调用一个方法没有任何好处

[ ^ ]



谢谢。

Hello Codeproject

Can anyone explain it to me why

Using either the Close method or the Dispose method is sufficient. You do not have to call one method after the other. There is no benefit to calling one method after the other.
[^]

Thanks.

推荐答案

因为在此case,Close和Dispose就任何使用过的资源做同样的事情 - 它们都释放它们(Dispose调用Close很可能,反之亦然)

Dispose的实现是实现IDisposable的所有类都需要(因此可以使用块在中实例化)但是没有任何停止类(例如Streams)在下添加相同的功能使用不同的名称 - 代码可以更加对称:

Because in this case, Close and Dispose do the same thing as far as any used resources are concerned - they both release them (There is a good chance that Dispose calls Close, or vice versa)
An implementation of Dispose is required by all classes that implement IDisposable (and thus can be instantiated in a using block) but there is nothing stopping classes (such as Streams for example) from adding the same functionality under a different name - and it is more symmetrical to have code that does this:
myStream.Open();
   ...
myStream.Close();

而不是

rather than

myStream.Open();
   ...
myStream.Dispose();


对于数据库连接,两者之间存在细微差别。

Dispose将完全释放数据库连接,因此无法重新打开。

关闭将关闭连接,但会保持连接字符串,连接超时等完整,因此您可以重新打开它。



但是关闭然后处置没有任何好处,因为已经处置已关闭。
For database connections, there's a small difference between both.
Dispose will release the database connection completely, so it can't be reopened.
Close will close the connection but will keep the connection string, connection timeout etc intact, so you can reopen it.

But there's no benefit in closing then disposing, as dispose already does the close.


这篇关于处理并关闭澄清的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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