Code snippet is as follows
public static string ToCompressedBase64(this string text)
{
using (var memoryStream = new MemoryStream())
{
using (var gZipOutputStream = new GZipStream(memoryStream, CompressionMode.Compress))
{
using (var streamWriter = new StreamWriter(gZipOutputStream))
{
streamWriter.Write(text);
}
}
return Convert.ToBase64String(memoryStream.ToArray());
}
}
As far as I know, if class contains field which is IDisposable then It should implement IDisposable itself and take care of disposal of the owned object, so with this assumptions, after disposal of streamWriter the gZipOutputStream and memoryStream will also be disposed. But we still need not disposed memoryStream to invoke toArray() method on It.
So the question is, Is calling ToArray() method on memoryStream at the end safe?