Click here to Skip to main content
15,919,423 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more: , +
How do i dispose managed and unmanged code using Dispose method in C#?

After reading some article i came to know that we need to use Dispose method to clear the garbage. See below code
C#
public class abc: IDisposable
{ 
    public void Dispose();
    protected virtual void Dispose(bool disposing);
}

C#
public void Dispose()
{
    Dispose(true);
    GC.SupressFinalize(this);
}

protected virtual void Dispose(bool disposing)
{
    if (!disposed)
    {
        if (disposing)
        {
            // Dispose managed resources.
        }

        // There are no unmanaged resources to release, but
        // if we add them, they need to be released here.
    }
    disposed = true;

    // If it is available, make the call to the
    // base class's Dispose(Boolean) method
    base.Dispose(disposing);
}

here we are calling Dispose method and from that calling Dispose(bool).After disposing we do not need Finalize so we are doing GC.SupressFinalize(this);
My doubt is,

Anyway we need to write the code to dispose the unmanaged\managed code correct? like below
C#
if (!disposed)
{
   if (disposing)
   {
       // Dispose managed resources. ---We need to write the code here to dispose the item right?
   }

   // There are no unmanaged resources to release, but
   // if we add them, they need to be released here.
}

then what is the use of Dispose method here? we can create any method(With some other name) and call it to released. why should we use IDisposable interface ?
I can do it in my custom code correct?
Posted
Updated 24-Feb-15 0:39am
v2
Comments
User-8621695 24-Feb-15 4:28am    
Have you tried with your custom code?

1 solution

First, you are not showing the whole implementation of IDisposable. You are missing two important pieces to the puzzle:
1) Object's finalizer method
2) The using statement[^]

The full disposable pattern should look like this:
C#
public void Dispose()
{
    Disposing(true);
}

private Dispose(bool disposing)
{
    if(disposing)
    {
        // release managed resources
        GC.SupressFinalize(this); // do not run finalizer, saves work for runtime
    }
    
    // release unmanaged resources
}

private ~MyClass() // the finalizer
{
    Dispose(false);
}

Note that the same method is called from both dispose and finalize. Also note you need to implement all this only if you use unmanaged resources. Without them it gets a lot more simple - you can put all your logic directly into Dispose() and forget about the rest.

Why you should use IDisposable? Because you can the use the using statement:
C#
using(var my = new MyClass())
{
    // my.Dispose() gets called at the end automatically
}

So why do you need a finalizer you ask? Because as the author of a class you cannot be sure that the programmers using your class will call dispose or use using. There is no way for you to enforce it. So if you use some unmanaged resources you want to make sure they get released properly. So what good is Dispose() if all can be taken care of automatically? Because generally you want to release resources as soon as possible, not wait for GC to kick in.

There are several good articles here on CodeProject about IDisposable pattern. The last one is great but very technical. Nevertheless I recommend it because of the explanation why IDisposable is actually not good.
* Understanding and Implementing IDisposable Interface - A Beginner's Tutorial[^]
* Implementing IDisposable and the Dispose Pattern Properly[^]
* IDisposable: What Your Mother Never Told You About Resource Deallocation[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900