public interface ISomeInterface : IDisposable
{
void SomeMethod();
}
public class MyClass : ISomeInterface
{
public void SomeMethod()
{
}
public void Dispose()
{
this.Dispose(true);
}
~MyClass()
{
this.Dispose(false);
}
protected virtual void Dispose(bool disposing)
{
if (disposing)
GC.SuppressFinalize(this);
}
}
{
void SomeMethod();
}
public class MyClass : ISomeInterface
{
public void SomeMethod()
{
}
public void Dispose()
{
this.Dispose(true);
}
~MyClass()
{
this.Dispose(false);
}
protected virtual void Dispose(bool disposing)
{
if (disposing)
GC.SuppressFinalize(this);
}
}
Code to execute:
MyClass myClass = new MyClass();
try
{
myClass.SomeMethod();
}
finally
{
myClass.Dispose();
myClass = null;
}
try
{
myClass.SomeMethod();
}
finally
{
myClass.Dispose();
myClass = null;
}
Please note that, if the object is of IDisposable; it is better to use using {....} block in C#. It internally takes care of the garbage collection.
No comments:
Post a Comment