3

Possible Duplicate:
Response.Redirect(“”) inside “using{ }”

Pretty much that. If I had, for example, a connection to the database in a using statement and somewhere inside that using statement I redirected to another page, would it dispose of my connection or would I have to manually call it first?

Community
  • 1
  • 1
proseidon
  • 2,235
  • 6
  • 33
  • 57
  • 3
    See: http://stackoverflow.com/questions/187189/response-redirect-inside-using?rq=1 –  Aug 17 '12 at 14:50

4 Answers4

7

Yes, it would absolutely call Dispose. The point of a using statement is that Dispose is called in a finally block, so the resource will be disposed whether the block completes normally or with an exception.

There's a slight wrinkle here in that IIRC, Response.Redirect throws a ThreadAbortException which will automatically rethrow if it's caught, but that shouldn't affect a finally block.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • 1
    What about @JasonZ's note in the linked question: `Calling Response.Redirect WILL NOT execute the finally block.` – mellamokb Aug 17 '12 at 14:52
  • @mellamokb: That *sounds* incorrect to me. Looking at the [linked MSDN page](http://msdn.microsoft.com/en-us/library/aa973248.aspx), it doesn't contain the text which has been quoted any more. There's a rather longer section on it, but it definitely doesn't say that it "WILL NOT execute the finally block". – Jon Skeet Aug 17 '12 at 14:56
  • Ya, I noticed that too. Multiple answers quoted that exact text, but when I google there's not a single reference on MSDN to that text. It must have existed at one time and was changed? **EDIT**: That exact quote did indeed exist at one time: http://web.archive.org/web/20080515002933/http://msdn.microsoft.com/en-us/library/aa973248.aspx. Very strange... did something change regarding this since 2008? – mellamokb Aug 17 '12 at 14:56
  • @JonSkeet, From http://msdn.microsoft.com/en-us/library/system.threading.threadabortexception.aspx, "When this exception is raised, the runtime executes all the finally blocks before ending the thread. Because the thread can do an unbounded computation in the finally blocks or call Thread.ResetAbort to cancel the abort, there is no guarantee that the thread will ever end." – Roger Aug 17 '12 at 14:57
  • @Roger: Exactly. So it should be okay, basically. – Jon Skeet Aug 17 '12 at 14:59
2

It will dispose the connection properly. using is just syntactic sugar for try/finally. If you want to confirm the behavior, replace the using for try/finally. It will jump to the finally before doing the redirect.

Icarus
  • 63,293
  • 14
  • 100
  • 115
0

It will dispose of your connection for you.

Sean Barlow
  • 588
  • 6
  • 11
0

There are a couple of cases where Dispose() won't be called:

The application going into an infinite loop from which it never returns. The application shutting down ungracefully. The using is in an iterator and the using has yet to be actually entered.

You know you haven't the first two, because your process is neither hanging nor crashed. You know you've the third because the redirect is inside the using so it can't be the case.

Jon Hanna
  • 110,372
  • 10
  • 146
  • 251