在的MemoryStream使用声明 - 我需要调用close() [英] MemoryStream in Using Statement - Do I need to call close()

查看:1561
本文介绍了在的MemoryStream使用声明 - 我需要调用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 that Dispose() will be called, which in turn calls the Close() 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屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆