473,413 Members | 2,053 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,413 software developers and data experts.

Disposing Collections

This whole dispose topic confuses me regarding managed and unmanaged
resources. I think that it was easier when you just cleaned up every object,
which brings me to my problem.

I have some classes that have Hashtables collections defined as class
variables.
In the past, when I was done with the class, I would remove all the items
from the collection and then set the collection to null.

In C# (.NET General), is this necessary anymore? For example, do I need to
implement a dispose method that would remove all the items from the
collection and set the collection to null. Is it enough to just set the
class instance to null and everything inside would be cleaned up?

Thanks in advance.
Greg

Nov 17 '05 #1
4 8931
No, you don´t need to implement the dispose method in that case.

- If a class has a Dispose method, you need to call it when you are done
with it. For example, if the objects that you store in the collection have
the Dispose method and the collection is the only reference to them, you
should dispose them before removing them from the collection.

- You only need to implement a Dispose method in your class when it wraps an
unmaneged resource, such as a Win32 handle, or a managed object that has a
Dispose method that your class don´t call automatically internally.

--

Best regards,

Carlos J. Quintero

MZ-Tools: Productivity add-ins for Visual Studio .NET, VB6, VB5 and VBA
You can code, design and document much faster.
Free resources for add-in developers:
http://www.mztools.com

"gmccallum" <gm*******@discussions.microsoft.com> escribió en el mensaje
news:0C**********************************@microsof t.com...
This whole dispose topic confuses me regarding managed and unmanaged
resources. I think that it was easier when you just cleaned up every
object,
which brings me to my problem.

I have some classes that have Hashtables collections defined as class
variables.
In the past, when I was done with the class, I would remove all the items
from the collection and then set the collection to null.

In C# (.NET General), is this necessary anymore? For example, do I need
to
implement a dispose method that would remove all the items from the
collection and set the collection to null. Is it enough to just set the
class instance to null and everything inside would be cleaned up?

Thanks in advance.
Greg

Nov 17 '05 #2
Thank you. Is there a way to tell in code if an object implements the
Dispose method without having to try...catch exceptions.
For example
[not meant to be c# code]
if (isdisposable(class1)) class1.dispose()

"Carlos J. Quintero [.NET MVP]" wrote:
No, you don´t need to implement the dispose method in that case.

- If a class has a Dispose method, you need to call it when you are done
with it. For example, if the objects that you store in the collection have
the Dispose method and the collection is the only reference to them, you
should dispose them before removing them from the collection.

- You only need to implement a Dispose method in your class when it wraps an
unmaneged resource, such as a Win32 handle, or a managed object that has a
Dispose method that your class don´t call automatically internally.

--

Best regards,

Carlos J. Quintero

MZ-Tools: Productivity add-ins for Visual Studio .NET, VB6, VB5 and VBA
You can code, design and document much faster.
Free resources for add-in developers:
http://www.mztools.com

"gmccallum" <gm*******@discussions.microsoft.com> escribió en el mensaje
news:0C**********************************@microsof t.com...
This whole dispose topic confuses me regarding managed and unmanaged
resources. I think that it was easier when you just cleaned up every
object,
which brings me to my problem.

I have some classes that have Hashtables collections defined as class
variables.
In the past, when I was done with the class, I would remove all the items
from the collection and then set the collection to null.

In C# (.NET General), is this necessary anymore? For example, do I need
to
implement a dispose method that would remove all the items from the
collection and set the collection to null. Is it enough to just set the
class instance to null and everything inside would be cleaned up?

Thanks in advance.
Greg


Nov 17 '05 #3
Since disposable objects should implement the IDisposable interface, you can
use: if (obj1 is IDisposable)

--

Best regards,

Carlos J. Quintero

MZ-Tools: Productivity add-ins for Visual Studio .NET, VB6, VB5 and VBA
You can code, design and document much faster.
Free resources for add-in developers:
http://www.mztools.com
"gmccallum" <gm*******@discussions.microsoft.com> escribió en el mensaje
news:1A**********************************@microsof t.com...
Thank you. Is there a way to tell in code if an object implements the
Dispose method without having to try...catch exceptions.
For example
[not meant to be c# code]
if (isdisposable(class1)) class1.dispose()

Nov 17 '05 #4


gmccallum wrote:
Thank you. Is there a way to tell in code if an object implements the
Dispose method without having to try...catch exceptions. For example
[not meant to be c# code]
if (isdisposable(class1)) class1.dispose()


IDisposable objects (usually) represents resouces which should be
released at-once when you are done using them. This introduces a concept
of "ownership" of the disposable.

You shouldn't Dispose() just because you are passes an IDisposable. only
the "owner" should do that.

Usually ownership is on the callstack, which fits the "using" idiom very
nicely, Example:

class Foo {
void WriteTo(Stream s) {
s.Write(...);
// just use stream, don't dispose
}
void f() {
using ( Stream s = new File.Open("foo") ) // "own" s
WriteTo(s);
}
}

Another case is where objects have "owned" members, they often become
resources themselves, becoming candidates for disposal:

class Bar: IDisposable {
Stream s;
public Bar() { s = new File.Open("foo"); }
public void Dispose() {
GC.SuppressFinalize(this);
if ( s != null ) {
s.Dispose();
s = null;
}
}
~Bar() {
// This code should not run in well-written programs
Errors.ForgottenDispose(this);
Dispose();
}
}
--
Helge Jensen
mailto:he**********@slog.dk
sip:he**********@slog.dk
-=> Sebastian cover-music: http://ungdomshus.nu <=-
Nov 17 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: Patrick De Ridder | last post by:
I have been looking at an example, and there is something I don't inderstand. Given: form1 calls form2 --------- Question: What is the use of having these lines in form2 --------------...
13
by: MuZZy | last post by:
Hi, Just wanted to make sure i get it right: consider this class: // =========== START CODE ============= class Test { private SqlConnection con = null; public void Connect() { con = new...
1
by: Tim T. | last post by:
I'm currently working on a report to forecast production for finished goods. The user can select one or more items to forecast. In addition, they may select one or more warehouses to view...
5
by: Chris | last post by:
I have a form that requires drawing custom lines on it. The color of the lines is suppose to be the same as the forcolor of the form. Am I doing this the most efficent and correct way? ...
13
by: Adam Right | last post by:
Hi, I am developing a financial application and you know that there are huge amount of data in these similar aplications. I have a MDIChild form and the other forms are opened in this form. For...
4
by: Sid Price | last post by:
Hello, I have a class of objects (Device) that are managed by another object (Devices) with a collection class (DeviceCollection) inherited from Collections.Hashtable. Each of the Device objects...
4
by: Peter Webb | last post by:
I am supposed to manually dispose of some instances, such as Brushes, right? I have a couple of questions: 1. I have the following code, and it works just fine: ...
29
by: Jerry Spence1 | last post by:
I'm rather confused as to whether something should be disposed of, or not. What is the general rule? How can you be sure of doing the right thing? I've heard about disposing unmanaged resources but...
3
by: medicinesoup | last post by:
I am using Visual Basic .Net 2005 professional. From what I understand, I should be disposing of any object that is an unmanaged resource. I understand the best way to tell if a resource is...
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:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

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.