GC Dispose method

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.");
}
}
 
G

Guest

Hi Maxim,
there are two reasons I can think of why you would want to use the
IDisposable interface rather than just adding a Dispose method or calling the
Dispose method a different name:

1. A method called Dispose is a standard way of telling someone who uses
your object that there are resources that should be freed in the object and
this method should be called. If you called your Dispose method something
else i.e. CleanUpStuff(), then someone using your object might not realize he
has to call this after he is finished with the object, on the other hand if
they see a method called Dispose they know they should call it after they
have finished with the object.

2. The using keyword. This keyword can be placed around a block of code and
will automatically call dispose on an object for you. You need to pass it an
object which implements the IDisposable interface otherwise it will not
compile i.e.

class MyObject : IDisposable
{
public void Dispose()
{
//dispose logic
}
}

public void Main()
{
MyObject x = new MyObject();


using(x)
{
//do some logic
Console.WriteLine(x.ToString());

//dispose will get called automatically, thanks to the using statement
}
}

If MyObject did not implement IDisposable the above code would not compile.

Hope that helps
Mark R Dawson


Mark R Dawson.
 
D

Daniel O'Connell [C# MVP]

Mark R. Dawson said:
Hi Maxim,
there are two reasons I can think of why you would want to use the
IDisposable interface rather than just adding a Dispose method or calling
the
Dispose method a different name:

1. A method called Dispose is a standard way of telling someone who uses
your object that there are resources that should be freed in the object
and
this method should be called. If you called your Dispose method something
else i.e. CleanUpStuff(), then someone using your object might not realize
he
has to call this after he is finished with the object, on the other hand
if
they see a method called Dispose they know they should call it after they
have finished with the object.

2. The using keyword. This keyword can be placed around a block of code
and
will automatically call dispose on an object for you. You need to pass it
an
object which implements the IDisposable interface otherwise it will not
compile i.e.


In addition to that, it will come in handy in situations where you test for
IDisposable. For example, while IEnumerator doesn't require IDisposable, C#
will call Dispose if the enumerator implements the interface(or atleast the
pattern, I forget what C# requires exactly). You'll see plugin architectures
or generic object pooling systems do that as well.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top