Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / March 2008

Tip: Looking for answers? Try searching our database.

release resources by using a using statement

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Tony Johansson - 30 Mar 2008 18:31 GMT
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(fullPathname))
{
   string line;
   while ((line = reader.ReadLine()) != null)
   {
       source.Text += line + "\n";
   }
}

//Tony
Arne Vajhøj - 30 Mar 2008 18:54 GMT
> Below I have a simple using construction. When this go out of scope which
> close will be called
[quoted text clipped - 12 lines]
>     }
>  }

It calls StreamReader Close, because what you have is an instance
of StreamReader. This is how polymorphism works.

Arne
Jon Skeet [C# MVP] - 30 Mar 2008 20:12 GMT
> Below I have a simple using construction. When this go out of scope which
> close will be called
[quoted text clipped - 12 lines]
>     }
>  }

It will actually call TextReader.Dispose() which will in turn call the
virtual Dispose(bool) method - which is overridden by StreamReader.

I don't see why you thought it "must be totally wrong" however - the
whole point of polymorphism is that if StreamReader *did* override
TextReader.Dispose() (which it can't in this particular case, as
TextReader.Dispose is non-virtual) then the StreamReader implementation
would be called.

Signature

Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet   Blog: http://www.msmvps.com/jon.skeet
World class .NET training in the UK: http://iterativetraining.co.uk


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.