Which one do I call?
Is it necessary to call both?
Will the other throw an exception if I have already called one of them?
Which one do I call?
Is it necessary to call both?
Will the other throw an exception if I have already called one of them?
You can use the
using
block for this. It will automatically callDispose
when it goes outside of its scope.Example:
Hope this helped.
the following code is Stream.Dispose from reflector as you can see, you don't need to close if you dispose (which is implicit when using using)
As a first solution, it's recommended to use using statements wherever possible. This is described here: http://msdn.microsoft.com/en-us/library/yh598w02.aspx
Coming to the question now, as others suggested in most .NET framework classes, there is no difference between Close() and Dispose() and it doesn't matter which of the two methods you call. You should call one but not both. However, there are exceptions.
For complete details, you can check here: https://blogs.msdn.microsoft.com/kimhamil/2008/03/15/the-often-non-difference-between-close-and-dispose/
Any of them.
No, either one is sufficient.
No, disposable pattern declares that subsequent calls to Dispose don't cause negative effects.
Use
using
block so that your object is disposed if its implementsIDisposable
interfaceIn .NET 3.5 (haven't checked other versions), methods are called in the following order when disposing a MemoryStream: