What is the purpose of dispose method in .NET?

You might already know that garbage collector does automatic memory management and clean up of resources. But there is a limitation.



Garbage collector releases memory occupied by managed resources i.e. resources that are managed in heap. But sometimes you might also use unmanaged resources. Few examples of such unmanaged resources include:

• Usage of stream reader to read a file
• Usage of stream writer to write contents into a file
• Establishing database connectivity

These unmanaged resources have to be released. Garbage collector gives you an option to release unmanaged resources by using Finalize method. This is implicitly called by the garbage collector before releasing an object and at that point, all unmanaged resources accessed by the object will be freed. Other than this, there is yet another option wherein the developer can explicitly release unmanaged resources. This is done using dispose method.

The dispose method is available in IDisposable interface and its signature is shown below:

public interface IDisposable {
void Dispose();
}

Consider that you have a text file called sample.txt and you prefer to read all lines of this text file using your C# program. For that, you will be using stream reader. Once the file is opened, read and then closed, you can release its memory using call to dispose method. This scenario is demonstrated below:

Content of sample.txt:


Now you attempt to create a C# program, read the contents of the file and release its memory. This is done using the code shown below:

class sampleClass {
public static void Main() {
StreamReader sampleReader = new StreamReader("sample.txt");
string eachLine;
Console.WriteLine(“Contents of sampleFile:”);
while ((eachLine = sampleReader.ReadLine()) != null) {
Console.WriteLine(eachLine);
}
sampleReader.Close();
sampleReader.Dispose();
}
}

Output of this code will be:

Contents of sampleFile:

This is a sample text file.
It is used to demonstrate usage of disposal method.
Enjoy reading it from your C# program.

In this program, you have explicitly called dispose method to perform the cleanup of StreamReader which handles the text file. C# provides a better solution wherein you will be using the statement “Using” in your code which will take care of automatic disposal of that resource.

Here is the modified example using the statement “Using”:

class sampleClass {
public static void Main() {
Using (StreamReader sampleReader = new StreamReader("sample.txt")) {
string eachLine;
Console.WriteLine(“Contents of sampleFile:”);
while ((eachLine = sampleReader.ReadLine()) != null) {
Console.WriteLine(eachLine);
}
}
}
}

This will give the same output and it also takes care of calling dispose method even if you have not explicitly called the method in the code shown above. When you compile this code, it will be internally converted into the code shown below:

class sampleClass {
public static void Main() {
StreamReader sampleReader = new StreamReader("sample.txt"));
try {
string eachLine;
Console.WriteLine(“Contents of sampleFile:”);
while ((eachLine = sampleReader.ReadLine()) != null) {
Console.WriteLine(eachLine);
}
}
finally {
sampleReader.Dispose();
}
}
}

The best way of implementing Dispose method is to use “Using” statement rather than explicitly calling the Dispose() method.

| 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.