locked
Default Destructor? RRS feed

  • Question

  • Hello, i just wanted to know if there is a default destructor in C#

    thanks,
    AJ.
    Sunday, August 16, 2009 10:51 PM

Answers

  • You need to look up the IDisposable interface and the dispose-pattern that managed applications use.
    http://blog.voidnish.com
    • Marked as answer by Figo Fei Tuesday, August 18, 2009 7:07 AM
    Sunday, August 16, 2009 11:43 PM
    Moderator
  • As long as you can't come up with anything to write in the destructor, you'll be in good shape if you just omit it.  This is not C++ anymore, you don't need a destructor to clean up memory.  To find out where you should write one, you ought to read Richter's "CLR through C#".  Required reading for C++ programmers, the rulez are very different.

    Hans Passant.
    • Marked as answer by Figo Fei Tuesday, August 18, 2009 7:07 AM
    Monday, August 17, 2009 12:52 AM
    Moderator
  • In general, if your class only uses managed resources and types that do not implement IDisposable, you will not need a Dispose() method.
    (Note that arrays are managed types that do not implement IDisposable.)

    If your class stores fields of types that implement IDisposable or if it retains handles to unmanaged objects (e.g. file handles obtains though P/Invoked calls to the Windows API), then you will need to implement IDisposable and free those resources inside Dispose().
    • Marked as answer by Figo Fei Tuesday, August 18, 2009 7:07 AM
    Monday, August 17, 2009 8:47 AM

All replies

  • Managed classes do not have the equivalent of C++ destructors. The closest equivalents are the finalizer/Dispose methods, and neither of them are auto generated in C#.


    http://blog.voidnish.com
    Sunday, August 16, 2009 11:10 PM
    Moderator
  • I believe not, although your question is somewhat vague.

    As far as I know you can only declare one 'destructor' yourself (per level of the class heirachy), which is more commonly called a 'finaliser' since technically there is no such thing as a destructor in C# (although there are some arguments about this as the C# documentation itself uses the term destructor sometimes). Since there can only ever be 0 or 1 destructors/finalisers (per inheritance level) there is no need for a 'default'.

    Classes where you have not defined a 'destructor' or 'finaliser' yourself do not have one as the garbage collector/.Net runtime do not need one... the only exception being where you have inherited from another class that has one defined, but in that case you cannot really access the destructor so it's existence of little importance unless you are implementing the dispose pattern in which case you should call GC.SuppressFinalise(this) to stop the destructor being called unnecessarily. Generally it's a good idea to do that even if you have no finaliser yourself (if your class is unsealed), in case someone else inherits from you and defines their own finaliser.

    So, in short, no, there is no 'default' finaliser.

    Sunday, August 16, 2009 11:12 PM
  • So i will have to add a destructor to every class?

    eg:
    public class Class1
    {
        // Constructor
        public Class1()
        {
             // Code
        }
    
        ~Class1()
        {
             // Cleanup code
        }
    }

    Cause i really want to get rid of a object after i finish with it...
    Does the CLR automatically clean up the objects which are unused?

    This is what im trying to do:
                try
                {
                    Xml xml = new Xml("console");
    
                    int height = xml.ReadInt32("console.height");
                    int width = xml.ReadInt32("console.width");
                    bool resize = xml.ReadBoolean("console.resize");
                    DebugMode = xml.ReadBoolean("console.debug_mode");
                    BeepEnabled = xml.ReadBoolean("console.beep_enabled");
    
                    if (resize)
                    {
                        Console.WindowHeight = Console.LargestWindowHeight - height;
                        Console.WindowWidth = Console.LargestWindowWidth - width;
                    }
    
                    xml = null;
                }
                catch (Exception ex)
                {
                    new Utilities.Debug.Log().WriteException(ex);
                }

    and i xml class  i just added:
    ~xml() { }
    Will it get cleaned up by the CLR?

    Sunday, August 16, 2009 11:42 PM
  • You need to look up the IDisposable interface and the dispose-pattern that managed applications use.
    http://blog.voidnish.com
    • Marked as answer by Figo Fei Tuesday, August 18, 2009 7:07 AM
    Sunday, August 16, 2009 11:43 PM
    Moderator
  • As long as you can't come up with anything to write in the destructor, you'll be in good shape if you just omit it.  This is not C++ anymore, you don't need a destructor to clean up memory.  To find out where you should write one, you ought to read Richter's "CLR through C#".  Required reading for C++ programmers, the rulez are very different.

    Hans Passant.
    • Marked as answer by Figo Fei Tuesday, August 18, 2009 7:07 AM
    Monday, August 17, 2009 12:52 AM
    Moderator
  • In general, if your class only uses managed resources and types that do not implement IDisposable, you will not need a Dispose() method.
    (Note that arrays are managed types that do not implement IDisposable.)

    If your class stores fields of types that implement IDisposable or if it retains handles to unmanaged objects (e.g. file handles obtains though P/Invoked calls to the Windows API), then you will need to implement IDisposable and free those resources inside Dispose().
    • Marked as answer by Figo Fei Tuesday, August 18, 2009 7:07 AM
    Monday, August 17, 2009 8:47 AM