Click here to Skip to main content
15,893,486 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi All,
Can anyone please clarify my dout about IDisposable intreface in C#.
We all know tat IDisposable interface is using for disposing unmanaged resources.
I have a class which contains following code. here i have implimented the Dispose method from IDisposable interface.

class ClassA:IDisposable
{
public ClassA()
{
Console.WriteLine("ClassBeingTested: Constructor");

}
private bool disposed = false;
Image img = null;



public Image Image
{

get { return img; }
}


~ClassA()
{
Console.WriteLine("ClassBeingTested: Destructor");
// call Dispose with false. Since we're in the
// destructor call, the managed resources will be
// disposed of anyways.
Dispose(false);

}

public void Dispose()
{
Console.WriteLine("ClassBeingTested: Dispose");
// dispose of the managed and unmanaged resources
Dispose(true);

// tell the GC that the Finalize process no longer needs
// to be run for this object.
GC.SuppressFinalize(this);
}

protected virtual void Dispose(bool disposeManagedResources)
{
// process only if mananged and unmanaged resources have
// not been disposed of.
if (!this.disposed)
{
Console.WriteLine("ClassBeingTested: Resources not disposed");
if (disposeManagedResources)
{
Console.WriteLine("ClassBeingTested: Disposing managed resources");
// dispose managed resources
if (img != null)
{
img.Dispose();
img = null;
}
}
// dispose unmanaged resources
Console.WriteLine("ClassBeingTested: Disposing unmanaged resouces");
disposed = true;
}
else
{
Console.WriteLine("ClassBeingTested: Resources already disposed");
}
}
// loading an image
public void LoadImage(string file)
{
Console.WriteLine("ClassBeingTested: LoadImage");
img = Image.FromFile(file);
}


}

What my doubt is why i need to impliment the Dispose method from IDisposable interface?. Instead of that i can create my own Dispose method in my class without inheriting from IDisposable interface which i have given below.

for the class below i haven't inherited my class from IDisposable interface. instead of that i created my own dispose method. this also works fine.
class ClassA
{
public ClassA()
{
Console.WriteLine("ClassBeingTested: Constructor");

}
private bool disposed = false;
Image img = null;



public Image Image
{

get { return img; }
}


~ClassA()
{
Console.WriteLine("ClassBeingTested: Destructor");
// call Dispose with false. Since we're in the
// destructor call, the managed resources will be
// disposed of anyways.
Dispose(false);

}

public void Dispose()
{
Console.WriteLine("ClassBeingTested: Dispose");
// dispose of the managed and unmanaged resources
Dispose(true);

// tell the GC that the Finalize process no longer needs
// to be run for this object.
GC.SuppressFinalize(this);
}

protected virtual void Dispose(bool disposeManagedResources)
{
// process only if mananged and unmanaged resources have
// not been disposed of.
if (!this.disposed)
{
Console.WriteLine("ClassBeingTested: Resources not disposed");
if (disposeManagedResources)
{
Console.WriteLine("ClassBeingTested: Disposing managed resources");
// dispose managed resources
if (img != null)
{
img.Dispose();
img = null;
}
}
// dispose unmanaged resources
Console.WriteLine("ClassBeingTested: Disposing unmanaged resouces");
disposed = true;
}
else
{
Console.WriteLine("ClassBeingTested: Resources already disposed");
}
}
// loading an image
public void LoadImage(string file)
{
Console.WriteLine("ClassBeingTested: LoadImage");
img = Image.FromFile(file);
}


}

So can any one tel me tat the reason behind of implimenting dispose method from IDisposable interface. I think everybody understands my question. i searched in google a lot. but i did not get a proper answer.So please clarify my doubt.

Regards
Lijo
Posted

1 solution

Implementing IDisposable is the only way to make use of the using keyword in C#.

Also, a method may be designed to accept the IDisposable interface as a parameter. If you do not implement that interface, then you cannot pass your class instance to that method.

Also, exposing IDisposable communicates to users of your component that it requires a call to Dispose() after using it.
 
Share this answer
 
v3

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