Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8065961
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T11:50:00+00:00 2026-06-05T11:50:00+00:00

Trying to understand when implementation of IDisposable is necessary: I wrote a little example.

  • 0

Trying to understand when implementation of IDisposable is necessary:

I wrote a little example.

  public class FileManager
  {
    private FileStream fileStream;
    public void OpenFile(string path)
    {
       this.fileStream = File.Open(path, FileMode.Open, FileAccess.Read);
    }
    public void CloseFile(string path)
    {
      if ( this.fileStream != null && this.fileStream.CanRead)
      {
        this.fileStream.Close();          
      }
      this.fileStream.Dispose();
    }
  }

// client
var manager = new FileManager();
manager.Open("path");
manager.Close("path");

Does this class need to implement IDisposable because it has a managed resource (FileStream) which holds onto an unmanaged resource (a file)? Or do I not have to implement IDisposable because I am cleaning up within the class?

Confused.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T11:50:01+00:00Added an answer on June 5, 2026 at 11:50 am

    For every instance of any type which implements IDisposable and might do so in a non-trivial fashion, it must at every moment be possible to identify how that instance will be Disposed. In most cases, this means that each IDisposable instance will have a well-defined owner, which is responsible for calling Dispose. In the case of the FileStream instance created by the class, your class is the owner, since nothing else will be able to Dispose it.

    Classes with fields that references to IDisposable instances which they own should almost always implement IDisposable, and use their Dispose method to Dispose the IDisposable objects they own. Your class has such a field; it should thus implement IDisposable.

    Whenever possible, a class which requires cleanup should be designed so that calling IDisposable.Dispose on it will suffice to perform any and all such cleanup as may be needed. In some cases, it may be impractical to perform cleanup without using some other method, but those cases are pretty rare. If one can design a class so that Dispose will take care of all necessary cleanup, one should do so.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to understand how the implementation of the Now attribute in DateTime works.
I am trying to understand Linux Kernel implementation of linked list and hash table.
I'm trying to understand a Spring 3.0 application which contains the following BeanPostProcessor implementation.
I have been trying to understand the internal implementation of java.util.HashMap and java.util.HashSet .
I'm trying to understand this comment from AES implementation code: /** * This program
I'm trying to understand the system call interface and implementation in the Linux kernel.
I'm trying to understand my options for calling a C# library implementation from unmanaged
Trying to understand radix sort for my data structures class. My teacher showed us
I'm trying to understand the implementation of List s in Scala. In particular I'm
I'm trying to understand the traverseImpl implementation in scalaz-seven : def traverseImpl[F[_], A, B](l:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.