在的MemoryStream使用声明 - 我需要调用close() [英] MemoryStream in Using Statement - Do I need to call close()
问题描述
当在使用语句中使用一个内存流,我需要调用close?例如是ms.Close()需要在这里?
使用(MemoryStream的毫秒=新的MemoryStream(字节阵列))
{
//东西
ms.Close();
}
没有,它不是。
使用
确保的 的Dispose()
将被调用,从而调用关闭()
方法
您可以假设各种流都得到由使用
语句关闭。
当您使用访问非托管资源,如StreamWriter的对象,一个好的做法是用一个using语句创建实例。 using语句自动关闭流时使用它的代码已经完成在对象上调用Dispose 。
块引用>When using a memory stream in a using statement do I need to call close? For instance is ms.Close() needed here?
using (MemoryStream ms = new MemoryStream(byteArray)) { // stuff ms.Close(); }
解决方案No, it's not.
using
ensures thatDispose()
will be called, which in turn calls theClose()
method.You can assume that all kinds of Streams are getting closed by the
using
statement.From MSDN:
When you use an object that accesses unmanaged resources, such as a StreamWriter, a good practice is to create the instance with a using statement. The using statement automatically closes the stream and calls Dispose on the object when the code that is using it has completed.
这篇关于在的MemoryStream使用声明 - 我需要调用close()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!