vivek
vivek

Reputation: 45

Is Dispose method automatically call the Finalize method internally?

Is Dispose method automatically call the Finalize method internally? if yes How?

Upvotes: 0

Views: 427

Answers (2)

Joel Coehoorn
Joel Coehoorn

Reputation: 416081

Dispose and Finalize are different things. Neither ever invokes the other.

Odds are your class does not even need a finalizer. The only time you should ever build a finalizer is when you are building a class to work with a whole new kind of unmanaged resource. As an example, if you're building a database access class that will make use of the existing ADO.Net providers for Sql Server, MySql, etc, you do not need a finalizer. You should not build a finalizer, because the finalizer already written for the underlying providers will take care of the clean up for you. But if you're building the ADO.Net provider for a brand new kind of database, or re-implementing your provider from scratch without relying at all on the existing code, you should implement a finalizer.

So, to repeat:

Finalizers are only for types that originate unmanaged resources. They should only be implemented for a particular kind of resource once, and only ever called by the framework itself.

Dispose is for wrapping other types that expose unmanaged resources. It should be implemented every time you hold an unmanaged resource, and called for every instance of the class that is created.

Neither have anything at all to do with memory, ever, except perhaps memory held by unmanaged code.

Upvotes: 2

Kyle Trauberman
Kyle Trauberman

Reputation: 25694

The Dispose method does not call the finalizer automatically. Also, the finalizer does not automatically call the Dispose method.

See this question for more info:

How does the IDisposable interface work?

Upvotes: 2

Related Questions