IDispose and Finalize relations in .NET

What is IDispose in .NET?

The IDispose interface gives the programmer way to free unmanaged resources and events handlers, in order to avoid memory leaks.

For example:

public class UnmanagedResourceUser :IDisposable

{

public void Dispose()

{

//Free unmanaged resources

}

}

In this way we have precise control over when the unmanaged resources are free.

Implementing the IDispose interface does not give the programmer control when the GC work and free the object from memory.

the using syntax in c#, gives the programmer elegant way to call the Dispose method. When the programmer uses the using syntax, the CLR call the Dispose method automatically on the object that implement the IDispose interface.

For example:

using (UnmanagedResourceUser resource = new UnmanagedResourceUser())

{

//Do something

}

//Here Dispose method call automatically.

What is Finalize in .NET?

The GC call the Finalize method moment before the object removed from memory. In other words by override the Finalize method, the programmer can free unmanaged resources in the object.

For Example:

public class UnmanagedResourceUser : IDisposable

{

//Override the Finalize method

~UnmanagedResourceUser()

{

//Free unmanaged resources

}

public void Dispose()

{

//Free unmanaged resources

GC.SuppressFinalize(this);

}

}

In most cases there is no need to override the Finalize method (destructor). The only reason to override Finalize method is if the class use interop PInvoke or complex COM object.

Why we have both? IDispose and Finalize.

If the programmer Implements only the IDispose interface, this does not guarantee that the Dispose method call when needed. Therefore in order to ensure the release of unmanaged resources the destructor (Finalize) is required.

If the Dispose method called, the programmer must avoid the destructor call by using the GC.SuppressFinalize.

GC.SuppressFinalize informs the CLR that is no longer necessary to call the Finalize method (destructor) because the Dispose method already released the unmanaged resources.

Links:

Implementing the Dispose pattern MSDN:

http://msdn.microsoft.com/en-us/library/b1yfkh5e(v=vs.110).aspx

Advertisements

About Idan Reuven

Idan is Microsoft Certified Proffesional Developer (MCPD) Idan Working As Senior Software developer. and specialize in professional application development with WPF, WCF, Entity Framework, XAML, HTML 5, Java Script , jQuery, MVC, MVVM, C#, C++, SQL Server Technologies.
This entry was posted in .NET 4.5 and tagged . Bookmark the permalink.

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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s