Possible Duplicate:
Proper use of the IDisposable interface
I tried to find an actual answer to my question from books, internet and on stackoverflow, but nothing has helped me so far, so hopefully I can word my issue exact enough to make sense.
In general I always found the same basic usage of how to free memory, which is approx. as follows and I do understand the code itself:
public class MyClass : IDisposable
{
bool disposed = false;
public void Dispose()
{
if (!disposed)
{
Dispose(true);
GC.SuppressFinalize(this);
disposed = true;
}
}
protected virtual void Dispose(bool disposing)
{
if (disposing)
{
//free managed ressources
}
// free other ressources
}
~MyClass()
{
Dispose(false);
}
}
It makes total sense the way the methods work. But now my question: Why do we need the base class IDisposable? In this code sample we define a method called Dispose()
. As I read everywhere that method is part of IDisposable, but we have just defined that method within MyClass
and this code would still work if we don't implement the base class IDisposable or am I wrong with this assumption?
I am not fully new to C# but there is still a lot for me to learn, so hopefully someone can lead me in the right direction here. I checked for another post with the same question, but couldn't find it, so if it does exist and it does answer my question please lead me there and I will delete this post.