C# – Disposable Using Pattern

c

  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()?

Best Answer

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).