Disposable Using Pattern

前端 未结 5 1244
天涯浪人
天涯浪人 2021-01-04 04:56
  using (FileStream fileStream = new FileStream(path))
  {
    // do something
  }

Now I know the using pattern is an implementation of IDisposable

相关标签:
5条回答
  • 2021-01-04 05:22

    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.

    0 讨论(0)
  • 2021-01-04 05:27

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

    0 讨论(0)
  • 2021-01-04 05:30

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

    0 讨论(0)
  • 2021-01-04 05:34

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

    0 讨论(0)
  • 2021-01-04 05:36

    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.

    0 讨论(0)
提交回复
热议问题