Search code examples
c#responsedispose

What's the difference of Response.Close() and 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) ?


Solution

  • Where both methods are provided the implementation of Dispose should call Close. It is a good idea to use the using statement to ensure that Disposeand therefore Close is called, even if there is an exception.

    In other words do this:

    using (Response response = ...)
    {
        // ...
    }
    

    Not this:

    Response response = ...;
    // ...
    response.Close(); // If there is an exception this might never get called!
    

    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.