BinaryWriter / Reader问题

编程入门 行业动态 更新时间:2024-10-27 18:29:46
本文介绍了BinaryWriter / Reader问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

考虑到BinaryWriter / BinaryReader对象在收集gc时关闭了底层的 流,下面的代码是正确的,如果它是 在执行WriteSomething方法后,阻止BinaryWriter对象垃圾收集的原因是什么? ---- 使用System; 使用System.IO; 命名空间测试 { class Test { static void Main(string [] args) { using(Stream output =(Stream)File.OpenWrite(" test.txt")) { WriteSomething(输出); WriteSomethingElse(输出); } } static void WriteSomething(Stream outputStream) { BinaryWriter writer = new BinaryWriter(outputStream); writer.Write(" Something"); } static void WriteSomethingElse(Stream outputStrea m) { BinaryWriter writer = new BinaryWriter(outputStream); writer.Write(" SomethingElse"); } } } ---

Considering that the BinaryWriter/BinaryReader object closes the underlaying stream upon being gc collected, is the following code correct, and if it is what is the reason preventing BinaryWriter object garbage collection after the WriteSomething method is executed? ---- using System; using System.IO; namespace Test { class Test { static void Main(string[] args) { using (Stream output = (Stream)File.OpenWrite("test.txt")) { WriteSomething(output); WriteSomethingElse(output); } } static void WriteSomething(Stream outputStream) { BinaryWriter writer = new BinaryWriter(outputStream); writer.Write("Something"); } static void WriteSomethingElse(Stream outputStream) { BinaryWriter writer = new BinaryWriter(outputStream); writer.Write("SomethingElse"); } } } ---

推荐答案

您的代码看起来很好。没有什么可以阻止垃圾收集器在WriteSomething()中收集BinaryWriter 。你为什么在乎?这一点超出了 的范围。 它没有Finalize()方法,因此当它变为。 Pete " Filip Strugar" <菲力@ sezampro_dot_yu>在留言中写道 新闻:eH ************** @ TK2MSFTNGP10.phx.gbl ... Your code looks fine. Nothing prevents the garbage collector from collecting the BinaryWriter in WriteSomething(). Why do you care? It''s out of scope at that point. It has no Finalize() method, so nothing is going to happen when it gets collected anyway. Pete "Filip Strugar" <fili@sezampro_dot_yu> wrote in message news:eH**************@TK2MSFTNGP10.phx.gbl... 考虑在收集gc时,BinaryWriter / BinaryReader对象关闭底层流,下面的代码是正确的,如果是什么原因阻止了WriteSomething方法之后的BinaryWriter对象垃圾收集执行? ---- 使用System; 使用System.IO; 命名空间测试 { 类测试 {静态无效Main(string [] args) {使用(Stream output =(Stream)File.OpenWrite(" test.txt" ;)) {WriteSomething(输出); WriteSomethingElse(输出); } 静态void WriteSomething(Stream outputStream) ) {/ / BinaryWriter writer = new BinaryWriter(outputStream); writer.Write(Something); } 静态void WriteSomethingElse(Stream outputStream) {/ / BinaryWriter writer = new BinaryWriter(outputStream); writer.Write(" SomethingElse"); } } } --- Considering that the BinaryWriter/BinaryReader object closes the underlaying stream upon being gc collected, is the following code correct, and if it is what is the reason preventing BinaryWriter object garbage collection after the WriteSomething method is executed? ---- using System; using System.IO; namespace Test { class Test { static void Main(string[] args) { using (Stream output = (Stream)File.OpenWrite("test.txt")) { WriteSomething(output); WriteSomethingElse(output); } } static void WriteSomething(Stream outputStream) { BinaryWriter writer = new BinaryWriter(outputStream); writer.Write("Something"); } static void WriteSomethingElse(Stream outputStream) { BinaryWriter writer = new BinaryWriter(outputStream); writer.Write("SomethingElse"); } } } ---

它会被处理掉吗? 当BinaryWriter / Reader被释放时,它会关闭底层流,所以 没有进一步写入(阅读)就可以了! < pd ****** @ hotmail>在消息中写道 新闻:19 ****************************** @ news.meganet新闻。 com ... Will it get disposed? When BinaryWriter/Reader gets disposed, it closes the underlying stream, so no further writing (reading) on it is possible! <pd******@hotmail> wrote in message news:19******************************@news.meganet news... 你的代码看起来很好。没有什么能阻止的垃圾收集器在WriteSomething()中收集BinaryWriter。你为什么在乎?那时它的范围超出了。 它没有Finalize()方法,因此无论如何都不会收集任何东西。 Pete " Filip Strugar" <菲力@ sezampro_dot_yu>在消息中写道新闻:eH ************** @ TK2MSFTNGP10.phx.gbl ... Your code looks fine. Nothing prevents the garbage collector from collecting the BinaryWriter in WriteSomething(). Why do you care? It''s out of scope at that point. It has no Finalize() method, so nothing is going to happen when it gets collected anyway. Pete "Filip Strugar" <fili@sezampro_dot_yu> wrote in message news:eH**************@TK2MSFTNGP10.phx.gbl... 考虑到BinaryWriter /在收集gc时,BinaryReader对象关闭 Considering that the BinaryWriter/BinaryReader object closes the

流,下面的代码是否正确,如果 stream upon being gc collected, is the following code correct, and if it

在WriteSomething方法执行后阻止BinaryWriter对象垃圾回收的原因是什么? 使用系统; 使用System.IO; 命名空间测试 {类测试 {静态无效Main(string [] args) { using(Stream output =(Stream)File.OpenWrite(" test.txt")) {WriteSomething(输出); WriteSomethingElse(输出); } 静态void WriteSomething(Stream outputStream) { BinaryWriter writer = new BinaryWriter(outputStream); writer.Write(Something); static void WriteSomethingElse(Stream outputStream) {/ / BinaryWriter writer = new BinaryWriter(outputStream); writer.Write(" SomethingElse"); } } } --- what is the reason preventing BinaryWriter object garbage collection after the WriteSomething method is executed? ---- using System; using System.IO; namespace Test { class Test { static void Main(string[] args) { using (Stream output = (Stream)File.OpenWrite("test.txt")) { WriteSomething(output); WriteSomethingElse(output); } } static void WriteSomething(Stream outputStream) { BinaryWriter writer = new BinaryWriter(outputStream); writer.Write("Something"); } static void WriteSomethingElse(Stream outputStream) { BinaryWriter writer = new BinaryWriter(outputStream); writer.Write("SomethingElse"); } } } ---

Filip Strugar< fili @ sezampro_dot_yu>写道: Filip Strugar <fili@sezampro_dot_yu> wrote: 它会被处理吗?当BinaryWriter / Reader被处理掉时,它会关闭底层流,所以不能再写(读)了! Will it get disposed? When BinaryWriter/Reader gets disposed, it closes the underlying stream, so no further writing (reading) on it is possible!

如果您*告知*它是Disposed,它只会被处置,或者使用 显式调用或使用using语句(或其他方法调用 调用它。) - Jon Skeet - < sk *** @ pobox> ; www.pobox/~skeet 如果回复小组,请不要给我发邮件

It would only be Disposed if you *told* it to be Disposed, either with an explicit call or with a using statement (or another method call which called it). -- Jon Skeet - <sk***@pobox> www.pobox/~skeet If replying to the group, please do not mail me too

更多推荐

BinaryWriter / Reader问题

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

发布评论

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

>www.elefans.com

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