IDisposable and "using" statements
According to several websites I have seen, and this MSDN page:
http://msdn.microsoft.com/en-us/library/yh598w02.aspx
when declaring a new object of a class that inherits from IDisposable, one should wrap that declaration in a "using" statement. OK, that sounds fair, but what classes implement IDisposable? From their very own example at the top of the MSDN page, they state that
"File and Font are examples of managed types..."
Well, if you look at the MSDN page for "File", you will see that the term "IDisposable" is not on that page even a single time.
Font, on the other hand, does list IDisposable as an implemented class on its MSDN page.
So, where is the list of all classes that inherit from IDisposable?
By the way, what do they mean by managed and unmanaged?
Skip
Re: IDisposable and "using" statements
Quote:
Originally Posted by
MrGibbage
Well, if you look at the MSDN page for "File", you will see that the term "IDisposable" is not on that page even a single time.
Skip
That's incorrect; File is a static class and, as would be expected, does not implement IDisposable. Font however does, and look; it's right in the declaration of the class at the top of the page!
http://msdn.microsoft.com/en-us/libr...wing.font.aspx
If a class implements IDisposable, it has a Dispose() method. That makes it pretty easy to tell, and once you have a bit more experience using .NET you will intuitively know which classes likely implement the interface. It is all about cleaning up unmanaged resources, so thing like file handles, GDI objects, etc. are all good candidates for IDisposable. You can also just right click the class in the IDE and choose "Go to definition."
Re: IDisposable and "using" statements
So I guess the MSDN page is wrong????
http://msdn.microsoft.com/en-us/library/yh598w02.aspx
In the remarks near the top, they say "File and Font are examples of managed types that access unmanaged resources..."
The File class most certainly does not implement IDisposable. But the code example at the bottom of the File msdn page (http://msdn.microsoft.com/en-us/libr...m.io.file.aspx) shows an example of placing the File declaration and methods in a using code block. So I don't know what to think.
Re: IDisposable and "using" statements
Quote:
Originally Posted by
MrGibbage
Yes. The reference to 'File' is incorrect. Since File is a static class, it won't implement IDisposable (as static classes do not implement IDisposable).
Msdn is usually correct, but there are 100's of 1000's of msdn pages - some are bound to be off a bit.
At any rate, don't sweat it. If you have a doubt about whether a class implements IDisposable, just create and instance of the class inside a using block. If it compiles, the class in question implements IDisposable.
Quote:
Originally Posted by
MrGibbage
The File class most certainly does not implement IDisposable. But the code example at the bottom of the File msdn page (
http://msdn.microsoft.com/en-us/libr...m.io.file.aspx) shows an example of placing the File declaration and methods in a using code block. So I don't know what to think.
The answer is simple. The File class is static and therefore only exposes static methods. Some of these static methods return objects that implement IDisposable.
This is the case of the examples listed in the link. Both StreamReader and StreamWriter implement IDisposable.
Code:
// Create a file to write to.
using (StreamWriter sw = File.CreateText(path))
{
sw.WriteLine("Hello");
sw.WriteLine("And");
sw.WriteLine("Welcome");
}
// Open the file to read from.
using (StreamReader sr = File.OpenText(path))
{
//
}
Notice that the static methods CreateText( ) and OpenText aren't returning File objects, they are returning objects that implement IDisposable (i.e. StreamWriter and StreamReader).