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 call Dispose
when it goes outside of its scope.
Example:
using (MemoryStream ms = new MemoryStream())
{
// Do something with ms..
}
// ms is disposed here
Hope this helped.
Use using
block so that your object is disposed if its implements IDisposable
interface
In .NET 3.5 (haven't checked other versions), methods are called in the following order when disposing a MemoryStream:
Calling only Dispose()
will do the trick =)
Which one do I call?
Any of them.
Is it necessary to call both?
No, either one is sufficient.
Will the other throw an exception if I have already called one of them?
No, disposable pattern declares that subsequent calls to Dispose don't cause negative effects.
Calling Close() will internally call Dispose() to release the resources.
See this link for more information: msdn