Is it always necessary to close streams or, because .net is managed code, will it be closed automatically as soon as it drops out of scope (assuming there are no exceptions
It is good practice to close your streams. Use the using
statement, and Dispose()
will be called when it falls out of scope (or if an exception is thrown), which will in turn close your stream.
static string SerialiseObjectToBase64(object obj)
{
using (var mstream = new MemoryStream())
{
...
return Convert.ToBase64String(mstream.ToArray());
}
}