Results 1 to 6 of 6

Thread: When to use a destructor

  1. #1

    Thread Starter
    Hyperactive Member divined's Avatar
    Join Date
    Aug 2004
    Location
    Thessaloniki, Greece
    Posts
    447

    When to use a destructor

    Hello everybody

    I`m bit confused as to when I need to implement a destructor for an object in C#. I`ve read that it is only required for unmanaged code. But what exactly does that mean?
    SteadFast!

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: When to use a destructor

    Unmanaged code is anything that doesn't use the .NET Framework, so basically COM and APIs. If your classes use unmanaged resources that need to be cleaned up, or they use other .NET classes that do, then you should implement a Dispose method. Managed classes that use unmanaged resources will basically be those that implement a Dispose method themselves, so your Dispose method may just call Dispose on a member variable or two.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3
    Lively Member deranged's Avatar
    Join Date
    Jun 2004
    Location
    TN
    Posts
    104

    Re: When to use a destructor

    Even if you're using managed code, Using a destructor can still be useful for if you want things to happen upon the disposal of a class. For example, if you want to decriment (or incriment) a value when your class moves on to a better place.

  4. #4

    Thread Starter
    Hyperactive Member divined's Avatar
    Join Date
    Aug 2004
    Location
    Thessaloniki, Greece
    Posts
    447

    Re: When to use a destructor

    The problem with the destructor in C#, is that it is only called when the garbage collector decides so. So you can`t base your code on the fact that the destructor will be called when your object goes out of scope.

    But what about a Dispose method? In what way is it different from a Destructor? Do I merely need to add a method named Dispose to my class? And when will it be called?
    SteadFast!

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: When to use a destructor

    The idea is that you implement a Dispose method and a Finalize method. The Dispose method should be called in code when the object is no longer needed. The Finalize method is a fail-safe to ensure that the Dispose method is called when the GC is ready to clean up the object whether the developer remembered to or not.

    http://msdn.microsoft.com/library/de...izeDispose.asp
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  6. #6

    Thread Starter
    Hyperactive Member divined's Avatar
    Join Date
    Aug 2004
    Location
    Thessaloniki, Greece
    Posts
    447

    Re: When to use a destructor

    Suppose the code below, which is from a very simple console demonstration app :

    Code:
    using System;
    using System.Text;
    
    namespace Testing
    {
    	/// <summary>
    	/// Summary description for Class1.
    	/// </summary>
    	/// 
    
    	public class Testing : IDisposable
    	{
    		// private member property
    		private bool is_disposed = false;
    
    		// overloaded virtual protected Dispose member method
    		protected virtual void Dispose(bool disposing)
    		{
    			if (!is_disposed)
    			{
    				if (disposing)
    				{
    					Console.WriteLine("Not in destructor, OK to reference other objects");
    				}
    				// Perform cleanup for this object.
    				Console.WriteLine("Disposing...");
    			}
    			this.is_disposed = true;
    		}
    
    		// virtual protected Dispose member method
    		public void Dispose()
    		{
    			Dispose(true);
    			// tell the GC not to finalize.
    			GC.SuppressFinalize(this);
    		}
    
    		~Testing()
    		{
    			Dispose(false);
    			Console.WriteLine("In destructor.");
    		}
    	}
    
    	class Tester
    	{
    		/// <summary>
    		/// The main entry point for the application.
    		/// </summary>
    		[STAThread]
    		static void Main(string[] args)
    		{
    			Testing tstObject = new Testing();
    			tstObject.Dispose();
    		}
    	}
    }
    In this code I can see that the Dispose method is being called. But what about teh Destructor? The Message in the destructor is not being written on screen before the app executes. Is there something wrong with the approach?
    SteadFast!

Posting Permissions

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



Click Here to Expand Forum to Full Width