I refer you to the source code again
protected virtual void Dispose(bool disposing)
{
if (disposing)
{
Stream stream1 = this.m_stream;
this.m_stream = null;
if (stream1 != null)
{
stream1.Close();
}
}
this.m_stream = null;
this.m_buffer = null;
this.m_decoder = null;
this.m_charBytes = null;
this.m_singleChar = null;
this.m_charBuffer = null;
No. Because when br goes out of scope and gets disposed it closes my stream. I don't want the stream disposed, hence the reason to topic started. What is needed is the leaveInnerStream switch. Remember innerstream could be a NetworkStream and I may have helper method that wrap that stream in a typical server program to operate on the NetworkStream. When these methods return they would close the NetworkStream unexpectedly.
--
William Stacey [MVP]
oh yeah, Flush(), my bad :-(
so what of
BinaryReader br = new BinraryReader(stream)
// do stuff
stream.Flush()
here you go, achieve the desired result.
doesn't it?
For the same reason you want to dispose of any object. You want its internal state to reflect it is disposed/closed. You don't want any future code that has a ref to the object to be able to call methods on the disposed stream (i.e. Read()). So I want to dispose of your wrapper stream, but you may not want to dispose of the innerstream - however you may want to Flush() it in the Dispose method like:
protected override void Dispose(bool disposing)
{
try
{
if (!disposing)
{
return;
}
if (this.leaveStreamOpen)
{
this.innerStream.Flush();
}
else
{
this.innerStream.Close();
}
}
finally
{
base.Dispose(disposing);
}
}-- William Stacey [MVP] "Lloyd Dupont said:
I disagree. What is your argument.
I disagree with you, what is the use of Dipose() anyway?
Anyway as you are impervious to the voice of reson, I copy the code from BinaryReader.Dispose() below (coming straight out of reflector) and I would be keen to know why "it is important" to call Dispose() on it.
protected virtual void Dispose(bool disposing)
{
if (disposing)
{
Stream stream1 = this.m_stream;
this.m_stream = null;
if (stream1 != null)
{
stream1.Close();
}
}
this.m_stream = null;
this.m_buffer = null;
this.m_decoder = null;
this.m_charBytes = null;
this.m_singleChar = null;
this.m_charBuffer = null;
}