What makes IDisposable a must-have during .NET development?

Tutorials No Comments

It’s been almost twelve years that .NET has came into existence. Disposing off objects has always remained a challenged for .NET developers. Thanks to the emergence of Idisposable, these developers can now implement, use and dispose off the objects in a hassle-free way. The all-new .NET 2.0 version has actually changed all the rules revolving around the finalizers, thereby helping people know everything that existed in .NET version 1.x that has become outdated as per th latest web development trends. IDisposable is a remarkable web interface that has made it possible for .NET developers to use unmanaged resources in a fabulous manner. To know more about this brand new interface, keep on reading this blog which will provide you useful information for all your upcoming .NET development projects.

 

A brief on history of IDisposable

While using unmanaged resources such as database connections and handles, it is absolutely essential for you to ensure that the same are held for least amount of time. Unlike in the case of C++, where these resources are released using a destructor, the .NET runtime uses a garbage collector(GC) for cleaning up and reclaiming the system memory that is being utilized by objects. Therefore, in .NET; the main point at which the objects are cleaned up is simply non-deterministic, thereby leading to deficiency of destructors for all managed objects.

 

IDisposable- Dealing with file locks

If you’re misusing IDisposable, then you’re bound to face the issue of file locks. In other words, under situations wherein you’re using an IDisposable instance without a ‘using’ clock and also without calling Dispose then you’re perhaps writing defects into the code. One of the best possible solutions to this problem is using the SQL server. Well, the SQLConnection class is IDisposable and if you’re using instances without disposing then the same hold onto connections and tend to starve the connection network, refraining other clients from connecting to the database.

 

IDisposable- Dealing with app crashes

One of the most common problems encountered by developers who don’t follow the best practices for IDisposable is the app crashing.  Since the un-disposed objects tend to stay on the heap of the app, they usually never get cleaned up which the memory space within the app expands until .NET becomes incompetent in allocating any more memory for the newly defined objects. All this leads to app crash.

 

IDisposable- A brief on its working

IDisposable interface comes with a single method called void Dispose() which is used for cleaning up the disposable object. It is also used for releasing all the unmanaged resources. To ensure the feasibility of calling the Dispose() method even under the situation where an exception takes place in the code existing between the object creation and Dispose method calling process, you must use the try-finally-construct as explained in the following lines:

var x = new X(); // X is a class that implements IDisposable
try
{
// do something with x
}

finally

{
x.Dispose();
}


Here’s a look at the code snippet that marks the successful implementation of IDisposable for the class that can be easily inherited and isn’t sealed:


public class MyDisposableType : IDisposable

{

~MyDisposableType()

{
// the finalizer also has to release unmanaged resources,
// in case the developer forgot to dispose the object.
Dispose(false);
}

public void Dispose()
{
Dispose(true);
// this tells the garbage collector not to execute the finalizer
GC.SuppressFinalize(this);
}

protected virtual void Dispose(bool disposing)
{

if (disposing)
{
// clean up managed resources
// dispose child objects that implement IDisposable

}

// clean up unmanaged resources

}

}


For classes that are derived from a disposable class, you don’t need to implement the entire IDisposable pattern again. All you can do is simply override the Dispose(bool disposing) method for adding some extra clean-up code. Here, just ensure to call the base-method as shown in the below code snippet:


public class MyDerivedDisposableType : MyDisposableType
{
protected override void Dispose(bool disposing)
{

try
{

if (disposing)
{
// clean up managed resources:
// dispose child objects that implement IDisposable
}
// clean up unmanaged resources
}

finally
{
// always call the base-method!
base.Dispose(disposing);

}

}

}

Wrapping Up

So with a deep understanding of IDisposable and the best practices for utilizing it, you can easily avoid all the frustrating issues that tend to consume a lot of your precious time and effort during .NET web development. Get going with finding and fixing defects using the very-beneficial IDisposable interface.

 

About The Author

Celin Smith is .NET web developer and blogger at Xicom who loves to  write about web & mobile apps. If you are looking to Hire ASP.NET Developer, you can get in touch with her or directly contact Xicom Technologies Ltd.

We really appreciate you for visiting PremiumCoding and reading this article! Now you might also want to check out our Themes here.