BinaryWriter's Close method

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Is it necessary to call the close method for every BinaryWriter that I
create? Or is it just a way to close the underlying stream? For example is it
OK to do the following:

private void WriteUnicodeStringToStream (Stream s, string string)
{
BinaryWriter bw = new BinaryWriter (s, Encoding.Unicode);
bw.Write (string);
}

As you can see, I don't close the BinaryWriter since I haven't necessarily
done the stream. Is it OK to do this?

As far as I can see, the Close method for a BinaryWriter doesn't do anything
than close the stream, so I assume it is safe... right?
 
If you do not close the binary writer then underlying stream will be open.
If close called on writer the stream will also be closed. check the MSDN
documentation.
 
Back
Top