我应该调用Close()或Dispose()用于流对象? [英] Should I call Close() or Dispose() for stream objects?

查看:114
本文介绍了我应该调用Close()或Dispose()用于流对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

类,如的StreamReader 的StreamWriter 等农具 IDispose 接口。这意味着,我们可以调用的Dispose()方法对这些类的对象。他们还定义了一个称为公共方法关闭()。现在,混淆了我,至于是什么,我应该叫一次我与对象做了什么?如果我叫什么都?

Classes such as Stream, StreamReader, StreamWriter etc implements IDispose interface. That means, we can call Dispose() method on objects of these classes. They've also defined a public method called Close(). Now that confuses me, as to what should I call once I'm done with objects? What if I call both?

我目前的code是这样的:

My current code is this:

using (Stream responseStream = response.GetResponseStream())
{
   using (StreamReader reader = new StreamReader(responseStream))
   {
      using (StreamWriter writer = new StreamWriter(filename))
      {
         int chunkSize = 1024;
         while (!reader.EndOfStream)
         {
            char[] buffer = new char[chunkSize];
            int count = reader.Read(buffer, 0, chunkSize);
            if (count != 0)
            {
               writer.Write(buffer, 0, count);
            }
         }
         writer.Close();
      }
      reader.Close();
   }
}

正如你所看到的,我已经写了使用()结构,自动调用的Dispose()方法上每个对象。但我也叫关闭()方法。是不是?

As you see, I've written using() constructs, that automatically calls Dispose() method on each object. But I also call Close() methods. Is it right?

使用流对象时,请给我建议的最佳实践。 : - )

Please suggest me the best practices when using stream objects. :-)

MSDN示例不使用使用()构造,并调用关闭()方法:

MSDN example doesn't use using() constructs, and call Close() method:

  • How to: Download Files with FTP

这是好?

推荐答案

一个快速跳转到Reflector.NET表明,在关闭()方法的StreamWriter 是:

A quick jump into Reflector.NET shows that the Close() method on StreamWriter is:

public override void Close()
{
    this.Dispose(true);
    GC.SuppressFinalize(this);
}

的StreamReader 是:

public override void Close()
{
    this.Dispose(true);
}

的StreamReader 的Dispose(BOOL处置)倍率:

protected override void Dispose(bool disposing)
{
    try
    {
        if ((this.Closable && disposing) && (this.stream != null))
        {
            this.stream.Close();
        }
    }
    finally
    {
        if (this.Closable && (this.stream != null))
        {
            this.stream = null;
            /* deleted for brevity */
            base.Dispose(disposing);
        }
    }
}

的StreamWriter 方法类似。

所以,在阅读了code很明显,你可以叫关闭()&安培; 的Dispose()往往你喜欢的任何顺序流。它不会以任何方式改变的行为。

So, reading the code it is clear that that you can call Close() & Dispose() on streams as often as you like and in any order. It won't change the behaviour in any way.

所以它归结为它是否是更具可读性使用的Dispose()关闭()和/或使用(...){...}

So it comes down to whether or not it is more readable to use Dispose(), Close() and/or using ( ... ) { ... }.

我个人的preference是使用(...){...} 时尽可能它可以帮助你不跑,应始终使用剪刀。

My personal preference is that using ( ... ) { ... } should always be used when possible as it helps you to "not run with scissors".

不过,虽然这有助于正确性,它确实降低可读性。在C#中,我们已经有关闭的大括号所以我们怎么知道哪一个实际执行上清溪收盘过多?

But, while this helps correctness, it does reduce readability. In C# we already have plethora of closing curly braces so how do we know which one actually performs the close on the stream?

所以,我认为这是最好这样做:

So I think it is best to do this:

using (var stream = ...)
{
    /* code */

    stream.Close();
}

这不会影响code的行为,但它确实有助于可读性。

It doesn't affect the behaviour of the code, but it does aid readability.

这篇关于我应该调用Close()或Dispose()用于流对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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