So I have the following code to convert a string to a MemoryStream
https://stackoverflow.com/a/1879470/2987066
public static Stream GenerateStreamFromString(string s)
{
var stream = new MemoryStream();
var writer = new StreamWriter(stream);
writer.Write(s);
writer.Flush();
stream.Position = 0;
return stream;
}
Would it be correct to dispose of the StreamWriter
as it implements IDisposable
but as we need to return the stream we use the leaveOpen
property on the StreamWriter
so would the following be correct to deal with memory leaks or is it not necessary?
public static Stream GenerateStreamFromString(string s)
{
var stream = new MemoryStream();
using(var writer = new StreamWriter(stream, leaveOpen: true))
{
writer.Write(s);
writer.Flush();
stream.Position = 0;
return stream;
}
}