constructor / destructur

  • Thread starter Thread starter Thomas
  • Start date Start date
T

Thomas

I'm new in csharp.
I create a SortedList with new objects.
I don't use this SortedList and their objects any more.

Have I to set all objects in the destructor to null?
Or do it the .Net framework for me?

Thanks Thomas
 
..NET uses a mark-and-sweep garbage collector. There are rare cases in
which you do need to manually dispose of things, but they are few and
far between.

After the variable that contains the reference to your SortedList goes
out of scope, the garbage collector will automatically clean it up.
 
Out of interest what exactly should you, and should you not, dispose of?
Is it always a good idea to call Dispose on anything that implements
IDisposable or are the rules more lenient?
 
thanks thomas

Uchiha Jax said:
Out of interest what exactly should you, and should you not, dispose of?
Is it always a good idea to call Dispose on anything that implements
IDisposable or are the rules more lenient?
 
Uchiha Jax said:
Out of interest what exactly should you, and should you not, dispose of?
Is it always a good idea to call Dispose on anything that implements
IDisposable or are the rules more lenient?

I believe it's a good idea to call Dispose on anything that implements
IDisposable if your code is the owner of it (and unfortunately that's
sometimes not clear).
 
not always...

The using statement disposes of the resource

A using statement of the form

using (R r1 = new R()) {
r1.F();
}

is precisely equivalent to

R r1 = new R();
try {
r1.F();
}
finally {
if (r1 != null) ((IDisposable)r1).Dispose();
}

Regards,
Nirosh.
 
Champika Nirosh said:
not always...

The using statement disposes of the resource

A using statement of the form

using (R r1 = new R()) {
r1.F();
}

is precisely equivalent to

R r1 = new R();
try {
r1.F();
}
finally {
if (r1 != null) ((IDisposable)r1).Dispose();
}

I'm not sure what your point is. Yes, the using statement is the
easiest way to call Dispose, but that's still calling Dispose...
 
My initial thought was that u implied to call Dispose manually.. and second
is "using" statement is not always call Dispose, if a null resource is
acquired, then no call is made to Dispose. Anyway I think your statement
covers this seemlessly

Regards,
Nirosh.
 
Back
Top