What is difference between dispose and destructor

Destructors 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 til ‘~’ in front of the class name (~MyClass() {}).

Since when exactly GC will call Finalize method is not know you might land up holding critical resources like database connections. it not advice to leave connection close statements in finalized methods, instead use dispose. 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. Dispose() is the recommended naming standard. You should put all cleanup code here rather than in the Finalizer

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s