我们是否需要关闭一个C#的BinaryWriter或BinaryReader在使用块? [英] Do we need to close a C# BinaryWriter or BinaryReader in a using block?

查看:268
本文介绍了我们是否需要关闭一个C#的BinaryWriter或BinaryReader在使用块?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有了这个代码:

using (BinaryWriter writer = new BinaryWriter(File.Open(ProjectPath, FileMode.Create)))
{
   //save something here
}

待办事项我们需要关闭的BinaryWriter?如果不是,为什么?

Do we need to close the BinaryWriter? If not, why?

推荐答案

只要这一切都在一个包裹使用块,然后你并不需要显式调用关闭

So long as it's all wrapped up in a using block then you don't need to explicitly call Close.

使用块将确保该对象被处置,而关闭的Dispose 方法是可以互换的的BinaryWriter 。 (在关闭方法只是调用的Dispose 幕后。)

The using block will ensure that the object is disposed, and the Close and Dispose methods are interchangeable on BinaryWriter. (The Close method just calls Dispose behind the scenes.)

这篇关于我们是否需要关闭一个C#的BinaryWriter或BinaryReader在使用块?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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