Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Writing our own Dispose method instead of using Idisposable

Tags:

c#

idisposable

After going through a lot of articles on IDisposable, I got confused about its usage. All the articles explain what is it and how to implement it, but I want to understand what we will miss if we don't have it.

Here is an example of an class implementing IDisposable. Often the use of dispose is shown as disposing a database connection.

public class Test : IDisposable
{
    public Test()
    {
        DatabaseConnection databaseConnection = new DatabaseConnection();
    }

    public void Dispose()
    {
        if (this.databaseConnection != null)
        {
            this.databaseConnection.Dispose();
            this.databaseConnection = null;
        }
    }
}

Although Dispose() is implemented, inside the Dispose() method, the dispose property of databaseConnection is used to free the connection (this.databaseConnection.Dispose();).

My question is why do we need to implement IDisposable in this case? We can directly call this.databaseConnection.Dispose() and free the connection. Why implement IDisposable when internally it also calls the Dispose() method of the object?

Alternative to the approach of IDisposable, we can implement a method Release() to free the memory.

public class Test
{
    public Test()
    {
        DatabaseConnection databaseConnection = new DatabaseConnection();
    }

    public void Release()
    {
        if (this.databaseConnection != null)
        {
            this.databaseConnection.Dispose();
            this.databaseConnection = null;
        }
    }
}

What is the difference between these two approaches? Do we really need IDisposable? I am looking forward to a concrete explanation.

like image 330
Piyush Avatar asked Oct 02 '10 11:10

Piyush


2 Answers

You are right, using your Release method you would get the exact same effect, provided you always remember to call it.

The reason why you should use Dispose / IDisposable for this sort of thing is consistency. All .NET developers will know about the IDisposable pattern, and a class being IDisposable indicates that you should dispose of it, and do it using the Dispose method. In other words, using the IDisposable pattern immediately tells another developer, that he should release the resources held by the class, and he should do it by calling the Dispose method.

Another benefit of implementing IDisposable is the using block, which works on any IDisposable class:

using(var t = new Test())
{
    // use t
}

Using the above code will result in t being Dispose()ed at the end of the using block. It is syntactic sugar for a try...finally block, but it tends to make this kind of code more concise and easier to read and write.

like image 51
driis Avatar answered Nov 12 '22 10:11

driis


  1. If your Release() worked correctly (it doesn't, but that's a different matter), then people would have to learn about it, and learn something else with another class, and so on.
  2. Your Release() could never be found programatically. It's possible to programatically call Dispose() when applicable with:

    if(obj is IDisposable)
      ((IDisposable)obj).Dispose();
    

While not often done, when it is done, it's vital.

It is sometimes useful to have a method like your Release() when it's possible someone might want to call it during object use. An example of this is Close() on Stream. Note here though that Stream.Dispose() still exists, and calls into Close().

like image 42
Jon Hanna Avatar answered Nov 12 '22 12:11

Jon Hanna