473,394 Members | 1,703 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

GC Dispose method

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
3 2056
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
Ok. Thanks a lot.
The second reason really sounds convincing.

Nov 17 '05 #3

"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: faktujaa | last post by:
Hi All, A small confusion. I have defined a connection class that has System.Data.IDbConnection as a member variable and implements IDisposable interface. I have implemented Dispose method to call...
4
by: RiteshDotNet | last post by:
..net Frame work 1. Dispose Method what it does ? A. who its call / when it calls ? B. Is it fire automatically ? c. When dispose method is call what it does ? D. Release a Object from memory or...
16
by: Daniel Mori | last post by:
If an object implements the IDisposable interface (regardless if its a framework object or a user object), should I always dispose of that object out of principle?
1
by: Billy | last post by:
Hello... I'm trying to make a database access class for an asp.net application. When I run my application, the Garbage Collecter doesn't seems to unload the memory attributed to my...
7
by: Scott M. | last post by:
In a typical class, do I need to indicate that it implements the IDisposable interface and then create a Dispose method that implements the Dispose required by the IDisposable interface or can I...
6
by: Teresa | last post by:
1) If I do want to keep an object alive throughout the live of an application, how can I ensure that the GC doesn't clean it up? 2a) How do I determine if an object is a managed or an unmanged...
156
by: Dennis | last post by:
Ok, I'm trying to dispose of every object that I create that has a dispose method based on advice from this newsgroup. However, I'm not sure how to dispose of the following object that was created...
5
by: Markus Stoeger | last post by:
Hi, I have a class similar to that: class MyClass : IDisposable { IDisposable obj1; IDisposable obj2; IDisposable obj3; MyClass() {
71
by: active | last post by:
In the main program I check to see if a certain form has been disposed. Does it make sense in that form's FormClosed event to do: Me.Dispose to make sure it is disposed the next time I check. Or...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.