E
Eric B.
Is it a bad idea to do both once I'm done with an object?
Eric B.
Eric B.
I always do close & dispose.
It might clear up the memory allocation.
But if you're inside a
private method, it will probably get destroy once you leave that
method.
But personally, I tend to always do both. To me, the two are semantically
different, even if they often wind up doing the same thing in practice.
Alvin said:If memory serves me correctly, some cases such as streams close calls
dispose, for most others dispose calls close. Call both is an exercise
in futility roughly equivalent to setting an int variable to 0 after
using it - well that's a stretch. Like Jon, I use the using statement
and let the framework figure out which call is prudent.
If you can reopen something then I would use Close(), but only if I intended
to reopen. If I intend to finish with the object completely I would ONLY
call Dispose. The reason is that if Dispose is implemented properly (and it
should be) then it will do everything Close() does plus possibly more.
No it won't.
No it won't.
Close() and Dispose() don't release memory. They may occasionally
release references that they may have to other objects, but unless
they then explicitly call GC.Collect() - which would be a bad idea -
that's not going to release any memory.
Personally I just use a "using" block which will automatically call
Dispose(), and don't worry about Close(). If I ever come across a type
which implements IDisposable but which I need to explicitly Close() in
common situations, I'll complain to the author.
Jon
For some classes, like a file stream, there is no difference between
calling Close and calling Dispose. You can't reopen the stream once it's
closed, you have to open a new stream. Those classes could call Dispose
from the Close method as you mention.
For some other classes, like a database connection, there is a
difference. If you close the connection, you can reopen it again, which
should use slightly less resources than creating a new one. Once you
have disposed the connection object, you can't use it any more.
Peter Duniho said:I close inside the finally block.
Absent a mandate in the .NET specifications, it's not guaranteed
behavior.
And it's worse to forget to close or dispose something that
needed to be than to do both when one would suffice. I find it annoying
to have to keep going back to the docs just to make sure I remembered the
behavior correctly for a specific class (heaven knows I mis-remember all
sorts of other things), so I just do both when both makes sense and leave
it at that.
Peter Duniho said:As bad as? Oh, come on. I think you're exaggerating, quite a lot.
Adding the extra call to Close() doesn't do anything like make C# "as bad
as" Java (not that I think Java is that bad anyway, but certainly I find
.NET/C# much nicer).
In any case, the kind of code I work on this doesn't come up much. Maybe
if I were dealing with these classes a lot I'd have a different opinion.
But I don't, and I don't.![]()
Brian said:One clarification...both Close and Dispose often do release unmanaged
memory though.