By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,110 Members | 990 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,110 IT Pros & Developers. It's quick & easy.

GC Dispose method

P: n/a
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.");
}
}

Nov 17 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
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.

"Maxim" wrote:
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.");
}
}

Nov 17 '05 #2

P: n/a
Ok. Thanks a lot.
The second reason really sounds convincing.

Nov 17 '05 #3

P: n/a

"Mark R. Dawson" <Ma*********@discussions.microsoft.com> wrote in message
news:84**********************************@microsof t.com...
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.
Nov 17 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.