C#

Understanding Dispose and Finalize in C#

In this post on Understanding Dispose and Finalize in C#, I will explain the two important methods related to the Garbage Collection. Basically, the garbage collection module of Common Language Runtime (CLR) in .NET Framework frees the programmer for memory management tasks. In fact, the garbage collector maintains three generations of objects – generation 0, generation 1, and generation 2. In order to read more about object generation and the GC class, read this post on How the .NET Framework Performs Garbage Collection.

Whenever, the objects become inaccessible, the Finalize() method is called. Although, Object class doesn’t provide an implementation of the Finalize() method, you can override it.

Finalize() Method

Basically, the Finalize() method is called before the garbage collection takes place. With the Finalize() method, the object tries to release resources. Additionally, the Finalize() method performs the cleanup of unmanaged resources. While we can implement the Finalize() method by defining a destructor in the class, it is not possible to explicitly call the Finalize() method. Therefore, it is called automatically just before the garbage collection occurs. As shown below, we define the destructor, which in turn gets translated into the Finalize() method.

class Prog
{   int i;
    public Prog()
    { i=100; }
    ~Prog(){}
}

It must be remembered that we can’t predict the exact time when the Finalize() method is called for a particular object. In any case, it is necessary to override the Finalize() method, if the class is using the unmanaged resources.

Dispose() Method

Besides, Finalize(), the Dispose() method also performs the cleanup process of the unmanaged resources. The classes which implement the IDispose interface need to implement the Dispose() method.

In short, we need to call the Dispose() method explicitly. In contrast, the system always calls the Finalize() method automatically.

Summary

In this article on Understanding Dispose and Finalize in C#, two important methods are discussed which the system uses for the cleanup process. Moreover, there is no order maintained by the system for the finalization process. In fact, both of these methods release unmanaged resources. Still, both are quite different. The Finalize() method is indeed a destructor while the Dispose() method is declared in the IDisposable interface.


Related Topics

A Beginner’s Tutorial on WPF in C#

Everything about Tuples in C# and When to Use?

Linear Search and Binary Search in C#

Creating Jagged Arrays in C#

Learning Indexers in C#

Understanding Method Parameter Modifiers in C#

Object Initializers in C#

Examples of Static Constructors in C#

When should We Use Private Constructors?

C# Practice Questions

C# Basic Examples

Private and Static Constructors in C#

Constructors in C#

C# Arrays

C# Examples

How to Create a C# Console Application

Creating Navigation Window Application Using WPF in C#

LINQ To SQL Examples

Understanding the Concept of Nested Classes in C#

How to Setup a Connection with SQL Server Database in Visual Studio

Examples of Extension Methods in C#

Learning All Class Members in C#

Access Modifiers in C#

C# Root Class – Object

KeyValuePair and its Applications

IEnumerable and IEnumerator Interfaces

IEqualityComparer Interface

New Features in C# 9

Generic IList Interface and its Implementation in C#

Examples of Connected and Disconnected Approach in ADO.NET

Leave a Reply

Your email address will not be published. Required fields are marked *