关闭和处置 ​​- 这称呼?

2023-09-02 01:17:57 作者:七度光芒﹎

在读线程是SqlCommand.Dispose就够了吗?和的关闭和处置WCF服务我想知道的类,如的SqlConnection或的几类从Stream类继承一个确实是问题,如果我收处置,而不是关闭?

Having read the threads Is SqlCommand.Dispose enough? and Closing and Disposing a WCF Service I am wondering for classes such as SqlConnection or one of the several classes inheriting from the Stream class does it matter if I close Dispose rather than Close?

推荐答案

根据微软的指导方针,这是一个很好的做法,当合适时提供关闭的方法。下面是从框架设计指南引文

I want to clarify this situation.

According to Microsoft guidelines, it's a good practice to provide Close method where suitable. Here is a citation from Framework design guidelines

考虑提供方法关闭(),除Dispose()方法,如果收盘在该地区的标准术语。这样做时,先进行关闭执行相同的处置......

Consider providing method Close(), in addition to the Dispose(), if close is standard terminology in the area. When doing so, it is important that you make the Close implementation identical to Dispose ...

在大多数情况下,关闭和处置的方法是等效的。在主要区别的关闭和处置的SqlConnectionObject的情况是:

In most of cases Close and Dispose methods are equivalent. The main difference between Close and Dispose in the case of SqlConnectionObject is:

应用程序可以调用关闭更多   超过一次。没有例外的是   产生的。

An application can call Close more than one time. No exception is generated.

如果您叫Dispose方法   SqlConnection对象的状态将是   复位。如果你试图调用任何   在处置的SqlConnection方法   对象,您将收到异常。

If you called Dispose method SqlConnection object state will be reset. If you try to call any method on disposed SqlConnection object, you will receive exception.

这是说:

如果您使用的连接对象的情况 时间,用废弃。 如果连接对象必须重复使用, 使用Close方法。 If you use connection object one time, use Dispose. If connection object must be reused, use Close method.
相关推荐