Response.Close() 和 Response.Dispose() 有什么区别?
从资源清理的角度来看,为什么有 Response.Close() 和 Response.Dispose() ,哪个更全面(调用另一个)?
From the resource clean-up perspective, why there are Response.Close()
and Response.Dispose()
and which one is more comprehensive (call the other one) ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果提供了两种方法,则
Dispose
的实现应调用Close
。最好使用 using 语句来确保调用Dispose
并因此调用Close
,即使存在异常也是如此。换句话说,这样做:
不是这样:
关闭和处置一个对象之间的一个区别是,当您处置一个对象时,通常不可能再使用该对象(尝试这样做可能会导致抛出 ObjectDisposedException),但是调用 Close 后,仍有可能使用该对象。
请注意,如果您正在谈论 ASP.NET,则通常不应在 Response 对象上调用 Close 或 Dispose。
Where both methods are provided the implementation of
Dispose
should callClose
. It is a good idea to use the using statement to ensure thatDispose
and thereforeClose
is called, even if there is an exception.In other words do this:
Not this:
One difference between closing and disposing an object is that when you dispose an object it usually is not possible to use the object any more (attempting to do so may cause an ObjectDisposedException to be thrown), but after calling Close it may be possible to still use the object.
Note that if you are talking about ASP.NET then you shouldn't normally call Close or Dispose on the Response object.
来自《开发类库设计指南》中的实现 Finalize 和 Dispose 以清理非托管资源< /a>
通常,每当可以打开或重新打开资源时,我都会看到 close,因为它为方法名称提供了很好的对称性。
From the Design Guidelines for Developing Class Library on Implementing Finalize and Dispose to Clean Up Unmanaged Resources
Typically I've seen close whenever the resource can be opened or re-opened, since it gives nice symmetry to the method names.
Response.Close()
关闭与客户端的套接字连接。Response.Dispose()
是实现 IDisposable 接口并释放分配资源的方法。我认为 Response.Close() 是从 Response.Dispose() 方法调用的。
有关更多详细信息,您可以使用 Reflector
Response.Close()
closes the socket connection to a client.Response.Dispose()
is method which implements IDisposable interface and releases allocated resources.I think
Response.Close()
is called fromResponse.Dispose()
method.For more detailed information you can use Reflector