Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the point of Finalize and Dispose methods in .NET? (see details before answering)

Tags:

c#

.net

I get the need to clean up resources during the teardown of an object, but I have always found the differences between Dispose, Finalize, and the destructor methods a bit confusing.

I found this great article that concisely describes the distinctions between them, that I am going to have to save for future reference:
"Difference between Destructor, Dispose and Finalize methods" - Sanjay Saini http://sanjaysainitech.blogspot.com/2007/06/difference-between-destructor-dispose.html

The fundamental question I am trying to ask here is this.

If a language offers destructors (for example C# [refuted]) what value do Dispose and Finalize add to the equation?

Am I just a curmudgeon that is used to the old school way of doing everything in the destructor, or is there something I am missing that is only possible by breaking the tear-down of an object into three parts?

UPDATE:
As noted in some of the replies, C# does not actually have destructors. The question may be moot at this point in recognition of that. When I read in the above referenced article that C# actually had a separate deconstructor (an error apparently), it threw me for a loop and I started wondering what the point of Dispose and Finalize would be if you had a final destructor to wrap up everything. I suppose that in a GC langauge like C# the concept of a single destructor to provide the denemount for an object doesn't make much sense.

Sorry for the downvotes on some of you guys, but a couple people didn't read the question carefully and thought I was asking about the difference between Dispose and Finalize, which really wasn't the point.

like image 228
JohnFx Avatar asked Nov 30 '22 07:11

JohnFx


2 Answers

The author of that blog post is a bit confused...

In C#, there is no such thing as a "destructor". Only Finalizers and IDisposable.

The ~ClassName() method is not called a "destructor". It is called a finalizer.

Dispose exists to release resources from code, where the finalizer exists to be called from the GC. Very often, the finalizer calls the Dispose() method, but the "Dispose Pattern" sets you up to only handle unmanaged resources from the finalizer.

You see, when the finalizer gets called, you are on a different thread, and any managed object you have is not necessarily valid. Because of this, if you call Dispose() from the finalizer, you should really be calling Dispose(false) which tells the "Dispose Pattern" to only dispose unmanaged resources.

Further, the "Dispose Pattern" suggests that when Dispose(true) is called, you should suppress the finalizer on that object.

like image 72
Brian Genisio Avatar answered Dec 05 '22 04:12

Brian Genisio


Only managed objects can be finalized automatically. If you want to offer implicit disposal of unmanaged objects, the Finalizer can be used. If you want to offer explicit control of disposal to a caller of your object, you can allow them to call Dispose.

I like this article.

like image 39
JP Alioto Avatar answered Dec 05 '22 05:12

JP Alioto