none
What should I implement in IDisposable class's Dispose()? RRS feed

  • Question

  • I made a class of IDisposable interface like following.

    public class ScanFolder : IScanFolder, IDisposable

    {

    //other methods

    public IEnumerable<string> GetFiles()
    {
        return Directory.EnumerateFiles(Destpath, "*.*", SearchOption.AllDirectories);
    }

    //other methods

    }

    However error occurs like following.


    Error	1	'CDM_Scan.ScanFolder' does not implement interface member 'System.IDisposable.Dispose()'	

    Do I have to implement Dispose() method in the ScanFolder class?

    If so, which code should I implement in it?

    public void Dispose()
    {
        //What should I do here?
    }




    • Edited by Jeff0803 Tuesday, May 1, 2018 11:14 PM
    Tuesday, May 1, 2018 10:58 PM

All replies

  • https://docs.microsoft.com/en-us/dotnet/standard/garbage-collection/implementing-dispose
    Wednesday, May 2, 2018 2:06 AM
  • Hello Jeff0803,

    >>Do I have to implement Dispose() method in the ScanFolder class?

    When you implement interface you must implement some methods. The exception indicates you must implement Dispose method that are defined in IDisposable interface.

    >>If so, which code should I implement in it?

    A Dispose method is designed to release unmanaged resources. Have you defined wrap unmanaged-resource class like file and pipe handles, registry handles, wait handles, or pointers to blocks of unmanaged memory. If not you don't need to implement IDisposable method because GC will release managed resource. If you want to free managed resource in dispose method, you could adopt general pattern for implementing the dispose pattern for a base class that overrides Object.Finalize from MSDN .

    using System;
    
    class BaseClass : IDisposable
    {
       // Flag: Has Dispose already been called?
       bool disposed = false;
       
       // Public implementation of Dispose pattern callable by consumers.
       public void Dispose()
       { 
          Dispose(true);
          GC.SuppressFinalize(this);           
       }
       
       // Protected implementation of Dispose pattern.
       protected virtual void Dispose(bool disposing)
       {
          if (disposed)
             return; 
          
          if (disposing) {
             // Free any other managed objects here.
             //
          }
          
          // Free any unmanaged objects here.
          //
          disposed = true;
       }
    
       ~BaseClass()
       {
          Dispose(false);
       }
    }

    using System;
    
    class DerivedClass : BaseClass
    {
       // Flag: Has Dispose already been called?
       bool disposed = false;
       
       // Protected implementation of Dispose pattern.
       protected override void Dispose(bool disposing)
       {
          if (disposed)
             return; 
          
          if (disposing) {
             // Free any other managed objects here.
             //
          }
          
          // Free any unmanaged objects here.
          //
          disposed = true;
          
          // Call the base class implementation.
          base.Dispose(disposing);
       }
    
       ~DerivedClass()
       {
          Dispose(false);
       }
    }

    Msdn has detailed info about dispose method from below link.

    https://docs.microsoft.com/en-us/dotnet/standard/garbage-collection/implementing-dispose

    Best Regards,

    Neil Hu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, May 2, 2018 2:27 AM
    Moderator
  • If you do not have anything special in your class, then you do not have to deal with IDisposable.

    Wednesday, May 2, 2018 5:48 AM