Search code examples
c#disposej#

c# windows form override dispose issue


I'm working on a projact where i convert j# UI code to C#. in the old code (j#) there was a class extends Form, and method:

public void dispose()
{
   super.dispose();
   components.dispose(); 
}

after changing the imports from: com.ms.wfc.ui to system.windows.form the compiler is asking me to change super.dispose() to super.Dispose(). (same with component).

My question is if i need to change the method above to Dispose as well? in the J# code it doesnt say if this is override or not, and i think if i dont change it, dispose() will not be call whan this form is close


Solution

  • You don't need to declare the Dispose method as override, it's enough if you change it to Dispose().

    public class MyClass : IDisposable
    {
        public void Dispose()
        {
            // Perform any object clean up here.
    
            // If you are inheriting from another class that
            // also implements IDisposable, don't forget to
            // call base.Dispose() as well.
        }
    }
    

    Here is a good tutorial on using Dispose in C#: http://www.codeproject.com/Articles/15360/Implementing-IDisposable-and-the-Dispose-Pattern-P