How can a finalize method be suppressed in .NET?

The finalize method can be suppressed in .NET by calling the method GC.SuppressFinalize(). By suppressing, you mean that the finalize method will not be triggered for that particular object by the garbage collector



Why is this suppression required? Normally releasing of unmanaged resources can happen in two ways. One being the finalize method and other being the dispose method. If you are releasing unmanaged resources using dispose method, then you have to ensure that garbage collector does not call finalize method.

If it triggers finalize method, then it is redundant and unnecessary call since the resources are already cleaned up using dispose method. Hence while implementing dispose method you will use this method GC.SuppressFinalize() to suppress the finalize method. This is demonstrated with an example which is shown below:

public class sampleClass {
public class sampleDisposeClass: IDisposable {
public System.IO.StreamReader sampleReader;
System.ComponentModel.Component sampleComponent =
new System.ComponentModel.Component();

bool disposeFlag = false;

public void Dispose() {
DisposeResources(true);
GC.SuppressFinalize(this);
}

private void DisposeResources(bool disposeManagedRes) {
if(!this.disposeFlag) {
if(disposeManagedRes) {
sampleComponent.Dispose();
}
if (sampleReader != null) {
sampleReader.Close();
}
}
disposeFlag = true;
}

~sampleDisposeClass() {
DisposeResources(false);
}
}

public static void Main() {
sampleDisposeClass obj = new sampleDisposeClass();
obj.sampleReader = new System.IO.StreamReader("sample.txt");
string eachLine;
while ((eachLine = obj.sampleReader.ReadLine()) != null) {
Console.WriteLine(eachLine);
}
Console.ReadLine();
}
}

Output of this example will contain the contents of sample.txt file.

In this example, you do the following:

• Created a class called sampleClass which has a nested class called sampleDisposeClass

• The nested class has implemented the IDisposable interface in order to implement the Dispose method.

• The nested class sampleDisposeClass has two resources as its members. The StreamReader object sampleReader is an unmanaged resource and the component sampleComponent is a managed resource. Both these resources have to be cleaned up using Dispose method.

• Define Dispose method such that it calls your newly created method DisposeResources. Pass the Boolean value true while triggering the later method. True signifies that managed resources have to be cleaned up.

• Inside Dispose method, you have to make a call to GC.SuppressFinalize to prevent finalization being done by the garbage collector using finalize method.

• Define the method DisposeResources such that it cleans up both managed resources and unmanaged resources used by the current object. Managed resource will be disposed only if the Boolean flag disposeManagedRes passed as a parameter to the method has its value to be true.

• Define the destructor for the sampleDisposeClass which will be triggered only if the Dispose method is not triggered. The destructor should clean up only unmanaged resources and not managed resources. Hence you call DisposeResources from inside the destructor with the parameter value as false to ensure that managed resources are not cleaned up explicitly through destructors.

• Outside the nested class and inside the sampleClass you define the Main method which creates an instance of the nested class sampleDisposeClass and accesses the unmanaged resource – the StreamReader instance.

| How do you prevent a class from overriding in .NET? | How are classes related to objects in .NET Application | How are Delegates different from Events in .NET? | How are system exceptions different from application exceptions in .NET? | How are Value Types different from Reference Types in .NET? | How can a finalize method be suppressed in .NET? | How can you call Stored Procedure in ADO.NET? | How can you force Dispose method to be called automatically in .NET? | How do you call a Base Class Constructor from Derived Class Constructor in .NET? | How do you connect your VB.NET application to SQL Server? | How do you implement Cloning in .NET? | How do you implement Façade Design Pattern in .NET? | How do you implement MVC Pattern in ASP.NET? | How do you install .NET Assembly in GAC? | How is shadowing different from overriding in .NET? | How to prevent a particular .NET DLL from being decompiled? | Illustrate Delay Signing Process of an Assembly in .NET? | What are Reference Types in .NET? | What are the advantages of C#? | What are the advantages of VB.NET? | What are the differences between Namespace and Assembly in .NET? | What are the similar features between class and structure in .NET? | What are Value Types in .NET? | What do you mean by mixed mode authentication in .NET? | What do you mean by Satellite Assembly in .NET? | What do you mean by shadowing in .NET? | What is CTS in .NET? | What is ILDASM in .NET? | What is Managed Code in .NET? | What is Manifest in .NET? | What is MSIL in .NET Framework? | What is the importance of finalize method in .NET? | What is the need for Visitor Pattern in C#? | What is the purpose of bindingRedirect tag in web.config file of .NET? | What is the purpose of CodeDom in .NET? | What is the purpose of dispose method in .NET? | What is the purpose of Ngen.exe in .NET? | What is the purpose of Strong Name in COM Components of .NET? | What is the purpose of virtual keyword in .NET? | What Object Oriented Principles can be incorporated in .NET Application? |


“Amazon and the Amazon logo are trademarks of Amazon.com, Inc. or its affiliates.”

| Privacy Policy for www.dotnet-guide.com | Disclosure | Contact |

Copyright - © 2004 - 2024 - All Rights Reserved.