M
Maxim
Hi!
According to documenation, if we need to release some umanaged
resources manually, we need to implement IDisposable interface with
single Dispose method. I am just wondering, what will happen if I just
create my own Dispose (or any other name) mehtod, without implementing
the IDisposable interface. In all the examples, which I found, this
Dispose method called from my user code (not by GC). What do I miss?
Thanks.
Bellow is the sample code from
Jesse Liberty article:
using System;
class Testing : IDisposable
{
bool is_disposed = false;
protected virtual void Dispose(bool disposing)
{
if (!is_disposed) // only dispose once!
{
if (disposing)
{
Console.WriteLine("Not in destructor, OK to reference
other objects");
}
// perform cleanup for this object
Console.WriteLine("Disposing...");
}
this.is_disposed = true;
}
public void Dispose()
{
Dispose(true);
// tell the GC not to finalize
GC.SuppressFinalize(this);
}
~Testing()
{
Dispose(false);
Console.WriteLine("In destructor.");
}
}
According to documenation, if we need to release some umanaged
resources manually, we need to implement IDisposable interface with
single Dispose method. I am just wondering, what will happen if I just
create my own Dispose (or any other name) mehtod, without implementing
the IDisposable interface. In all the examples, which I found, this
Dispose method called from my user code (not by GC). What do I miss?
Thanks.
Bellow is the sample code from
Jesse Liberty article:
using System;
class Testing : IDisposable
{
bool is_disposed = false;
protected virtual void Dispose(bool disposing)
{
if (!is_disposed) // only dispose once!
{
if (disposing)
{
Console.WriteLine("Not in destructor, OK to reference
other objects");
}
// perform cleanup for this object
Console.WriteLine("Disposing...");
}
this.is_disposed = true;
}
public void Dispose()
{
Dispose(true);
// tell the GC not to finalize
GC.SuppressFinalize(this);
}
~Testing()
{
Dispose(false);
Console.WriteLine("In destructor.");
}
}