我们是否需要关闭一个C#的BinaryWriter或BinaryReader在使用块?

编程入门 行业动态 更新时间:2024-10-27 06:29:08
本文介绍了我们是否需要关闭一个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在使用块?

本文发布于:2023-11-09 10:37:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1572156.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:BinaryWriter   BinaryReader

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!