IDisposable and "using" statements
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4

Thread: IDisposable and "using" statements

  1. #1
    Join Date
    Apr 2011
    Posts
    58

    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

  2. #2
    Join Date
    Jun 2008
    Posts
    2,477

    Re: IDisposable and "using" statements

    Quote Originally Posted by MrGibbage View Post
    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."

  3. #3
    Join Date
    Apr 2011
    Posts
    58

    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.

  4. #4
    Arjay's Avatar
    Arjay is offline Moderator / MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    11,310

    Re: IDisposable and "using" statements

    Quote Originally Posted by MrGibbage View Post
    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..."
    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 View Post
    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).

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center