Pages

Showing posts with label IDisposable. Show all posts
Showing posts with label IDisposable. Show all posts
Thursday, December 29, 2011

Why to implement IDisposable interface?

0 comments


Assuming you have created a Class in your system. Which is an appropriate way of notifying the class that the object is going out of scope?

By implementing the IDisposable interface, we will be forced to implement the Dispose method which can handle the notification and dispose the objects using unmanaged resources. The Using block will ensure that the Dispose method will get called at the end of the block. Otherwise another approach is that you would need to implement the Dispose method and call it manually right before the object went out of scope, which is partially correct, does not handle the problem of exceptions. If you wanted to use second approach you would have to call the Dispose method in a finally block using the try/catch/finally mechanism, which is, in fact, what the using block does for you.

Continue reading →