public static byte[] Compress(byte[] data)
{
using (MemoryStream ms = new MemoryStream())
{
using (DeflateStream ds = new DeflateStream(ms, CompressionMode.Compress))
{
ds.Write(data, 0, data.Length);
ds.Flush();
}
return ms.ToArray();
}
}
上面的函数中会关闭内存流吗?或者将内存流分配给数组并返回数组是否更好?
public static byte[] Compress(byte[] data)
{
byte[] compressedData;
using (MemoryStream ms = new MemoryStream())
{
using (DeflateStream ds = new DeflateStream(ms, CompressionMode.Compress))
{
ds.Write(data, 0, data.Length);
ds.Flush();
}
compressedData= ms.ToArray();
}
return compressedData;
}
哪些代码可以更好地优化内存使用?