Author: Kamliesh Nadar Member Level: Gold Member Rank: 0 Date: 05/Aug/2004 Rating:  Points: 2 |
HI,
All these things are used to perform object cleanup, but they are all a little different.
Destructors: These are called by the CLR when the Garbage Collector destroys an object. In VB.NET it is called the Finalize() method. In C# it is declared by putting a tilda '~' in front of the class name (~MyClass() {}). Destructors aren't the best place to put clean up code. This is because it is only called by the GC and you can't determine when it will be called (you can force it to be called, but that's not recommended). Thus, if you open a database and you put the cleanup code in the destructor, that database connection could potentially be open for the entire life of the project. Or it might only stay open for only a moment, but you can't control this. The best rule of thumb is to leave Destructors alone until you get into more advanced programming.
Dispose methods: The Dispose() method is simply a method you call manually to clean up your object. In reality, this is just another method of your class and you can call it whatever you terminate the object. If you are a VB6 programmer, just think that whereever you would set 'object=Nothing' now just call the Dispose() method. You should put all cleanup code here rather than in the destructor. Whenever you open an object that is resource intensive, you should call its Dispose() method. As far as the method name is concerned, it's a standard to call it Dispose() and you should really stick to that. Ideally, you will also have your object implement the IDispose interface. This is beneficial in C# if you are accessing a class with the 'using' statement b/c it automatically calls the Dispose() method when it finishes. As far your question regarding the DisposeObject() method, I personally haven't heard of it before. But I have to assume that it is just another name for the Dispose() method. If you see someone using DisposeObject() in C# or VB.NET I suggest that you don't adopt that habit yourself since Dispose() is the recommended naming standard.
Regards,
|
Author: Abhishek Member Level: Silver Member Rank: 0 Date: 05/Aug/2004 Rating:  Points: 2 |
Thanks Kamlesh info is useful . Even i am talking about Dispose() method and not DisposeObject(). Still some more doubts ... 1) So does this means that destructor and Finalize is something which is same i.e. Finalize in VB.NET is equivalent to destructor in C#.NET. There's no significance of a Finalize method in C#?? 2) I had read somewhere that you should cleanup the unmanaged resources used in the class in Dispose method and give a call to it explicitly. Is that true. 3) Is Dispose method called by garbage collector in any case ? 4) What will happen if i've some unmanaged resources in a class and i do not clean them up in any of the cleanup methods?
|
Author: Muralidharan Ramakrishnan Member Level: Gold Member Rank: 0 Date: 05/Aug/2004 Rating:  Points: 2 |
Hi
In the managed world we are mostly relieved of finalizing objects. MSDN recommends to call dispose only if the object is really resource intensive. If you use unmanaged com objects then you must make sure they are propery terminated since GC dosn't take care of that.
Calling finalize directly has got a performance cost. Have a look at this code project articles
http://www.codeproject.com/dotnet/GC_101.asp http://www.codeproject.com/dotnet/GC_102.asp
Thanks and Regards Murali
|
Author: DCPL - Ram Member Level: Diamond Member Rank: 20 Date: 22/Aug/2008 Rating:  Points: 6 |
Finalize : 1.Finalize() is called by the runtime 2.Is a destructor, called by Garbage Collector when the object goes out of scope. 3.Implement it when you have unmanaged resources in your code, and want to make sure that these resources are freed when the Garbage collection happens.
Dispose : 1.Dispose() is called by the user 2.Same purpose as finalize, to free unmanaged resources. However, implement this when you are writing a custom class, that will be used by other users. 3.Overriding Dispose() provides a way for the user code to free the unmanaged objects in your custom class.
Verify these links for more ideas.. http://kyapoocha.com/aspnet-interview-questions/what-is-the-difference-between-finalize-and-dispose/ http://www.allinterview.com/showanswers/28556.html
Regards Sridhar R Nothing is illegal, Until You Get Caught With Tears...Sridhar R
|
Author: Bunty Member Level: Diamond Member Rank: 15 Date: 21/Sep/2008 Rating:  Points: 6 |
Hi,
Following the difference between Dispose and Finalize method,
1>CLR uses the Dispose and Finalize methods for performing garbage collection of runtime objects of .Net applications.
2>Clr has a Garbage Collector(GC) which periodically checks for unused and unreferenced objects in Heap.It call Finalize() method to free the memory used by such objects.
3>Dispose is another method which is invoked by Garbage Collector to release the memory occupied by an object.Dispose method needs to be explicitly called in code for removing an object from Heap.
4>Dispose method can be invoked only by the classes that IDisposable interface.
Regards S.S.Bajoria
Thanks & Regards
Bunty
|