14
  using (FileStream fileStream = new FileStream(path))
  {
    // do something
  }

Now I know the using pattern is an implementation of IDisposable, namely that a Try/Catch/Finally is set up and Dispose is called on the object. My question is how the Close method is handled.

MSDN says that it is not called, but I have read otherwise.

I know that the FileStream inherrits from Stream which is explained here. Now that says not to override Close() because it is called by Dispose().

So do some classes just call Close() in their Dispose() methods or does the using call Close()?

Ty.
  • 3,888
  • 3
  • 24
  • 31

5 Answers5

18

The using statement only knows about Dispose(), but Stream.Dispose calls Close(), as documented in MSDN:

Note that because of backward compatibility requirements, this method's implementation differs from the recommended guidance for the Dispose pattern. This method calls Close, which then calls Stream.Dispose(Boolean).

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • 2
    aww dammit, I've been Sleeted – George Mauer Feb 04 '09 at 22:30
  • Alright, this is what I thought from the documentation. Thanks Jon. – Ty. Feb 04 '09 at 22:32
  • 2
    @George: Don't you mean Skeeted? – Jeff Yates Feb 04 '09 at 22:33
  • 2
    And also I apparently can't type. – George Mauer Feb 04 '09 at 22:33
  • btw: I've seen weird bugs before where filestreams opened with a using statement are still locked by the file system long after even the program quits. Any thoughts on that? – Joel Coehoorn Feb 04 '09 at 22:34
  • Jon, I read this on your CSHARP faq today; what a coincidence! P.S. I just ordered your book C# in Depth! looking forward to it. – Michael G Feb 04 '09 at 22:35
  • @Joel: Sounds unlikely to me. I'd need to see evidence. If it's locked after the *program* has quit, it would have to be an OS bug, or a different process. Virus scanner perhaps? That can certainly have nasty effects. – Jon Skeet Feb 04 '09 at 22:35
  • 1
    @Michael: Cool, please send me feedback on the book. I love hearing from readers - in particular, please let me know if you spot any mistakes, however trivial! (Likewise any suggestions for clearer explanations...) – Jon Skeet Feb 04 '09 at 22:37
  • @Joel That is actually what this is related to. My guess is that it has more to do with static methods than strange behavior from the using statements. – Ty. Feb 04 '09 at 22:37
  • @Joel: are you sure that the program wasn't still running? Have you checked the task manager to confirm that? If the program had improperly terminated threads, it may have 'hang' without you noticing it. – arul Feb 04 '09 at 23:30
  • using is an alternative of try and finally block, but finally doesn't execute always (in case of unhandled exception), So using will also not work in case of unhandled exception? – Shobhit Walia Jun 23 '19 at 16:36
  • @ShobhitWalia: Could you clarify what you mean? For example, https://gist.github.com/jskeet/56a1742ca2f7247c31503996c43216f9 prints both "In try block" and "In finally block". – Jon Skeet Jun 23 '19 at 17:22
  • @JonSkeet, As far as I know finally block will never execute in few condition, for example when (stackoverflow exception occur), since using is just a abstraction of try and finally the same will be true in that case, Is my statement correct? – Shobhit Walia Jun 24 '19 at 02:45
  • @johnSkeet, As you already shared using statement only knows about Dispose method, Is there any way in which I can let him know the other user defined function? – Shobhit Walia Jun 24 '19 at 02:52
  • @ShobhitWalia: (Firstly, please don't email me - keep the discussion here.) StackOverflowException is *very* special - your previous comment implied this was for unhandled exceptions *in general*. As far as I know there's no way of performing any kind of recovery in the face of a StackOverflowException. And no, the `using` statement can't be configured to call a different method. – Jon Skeet Jun 24 '19 at 05:25
  • Ok @JonSkeet I will never email you. – Shobhit Walia Jun 24 '19 at 13:53
7

using calls Dispose() only. The Dispose() method might call Close() if that is how it is implemented.

George Mauer
  • 117,483
  • 131
  • 382
  • 612
2

Close() is not part of the IDisposable interface so using has no way to know whether it should be called or not. using will only call Dispose(), but intelligently designed objects will close themselves in the Dispose() method.

Misko
  • 2,044
  • 12
  • 15
1

I don't think the using calls Close(), it would have no way of knowing that it should call that particular function. So it must be calling dispose, and that in turn is calling close.

Mark Rogers
  • 96,497
  • 18
  • 85
  • 138
0

In .Net classes Close() call Dispose(). You should do the same.

Jacek Ławrynowicz
  • 2,670
  • 2
  • 23
  • 23