release resources by using a using statement

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Tony Johansson

    release resources by using a using statement

    Hello!

    Below I have a simple using construction. When this go out of scope which
    close will be called
    is the one in TextReader or the one in StreamReader

    It must be the one in TextReader otherwise it's very strange.
    The reson I ask it that a book called step by step C# 2005 is saying that
    close in StreamReader will be called which must be totally wrong.

    using (TextReader reader = new StreamReader(fu llPathname))
    {
    string line;
    while ((line = reader.ReadLine ()) != null)
    {
    source.Text += line + "\n";
    }
    }

    //Tony


  • =?ISO-8859-1?Q?Arne_Vajh=F8j?=

    #2
    Re: release resources by using a using statement

    Tony Johansson wrote:
    Below I have a simple using construction. When this go out of scope which
    close will be called
    is the one in TextReader or the one in StreamReader
    >
    It must be the one in TextReader otherwise it's very strange.
    The reson I ask it that a book called step by step C# 2005 is saying that
    close in StreamReader will be called which must be totally wrong.
    >
    using (TextReader reader = new StreamReader(fu llPathname))
    {
    string line;
    while ((line = reader.ReadLine ()) != null)
    {
    source.Text += line + "\n";
    }
    }
    It calls StreamReader Close, because what you have is an instance
    of StreamReader. This is how polymorphism works.

    Arne

    Comment

    Working...