BinaryWriter / Reader问题 [英] BinaryWriter/Reader question

查看:73
本文介绍了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.com>在消息中写道

新闻: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.com> wrote in message
news:19******************************@news.meganet news.com...
你的代码看起来很好。没有什么能阻止
的垃圾收集器在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.com> ;
http://www.pobox.com/~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.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too


这篇关于BinaryWriter / Reader问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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