XmlDocument, close and dispose

M

Mark Rae

Hi,

Can someone settle an argument, please...

Since the XmlDocument class has neither a .Close() method nor does it
implement IDisposable, is there any point in even setting it to null...?
E.g.

XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load("<filespec>");
// retrieve an XmlNode object and change its InnerText property
xmlDoc.Save("<filespec>");

At this point, would the line below be of any benefit...?

xmlDoc = null;

Any assistance gratefully received.

Mark
 
P

Peter Duniho

[...]
At this point, would the line below be of any benefit...?

xmlDoc = null;

My understanding is that the answer is no, in the case of a local variable
as you've implied in the code you posted. Perhaps someone has more
details, but I gather that the compiler "knows" that the variable xmlDoc
is not used beyond whatever point is the last place it's used, and the
object is marked as eligible for garbage collection at that point.
Setting the variable to null doesn't help.

Pete
 
J

Jon Skeet [C# MVP]

Mark Rae said:
Can someone settle an argument, please...

Since the XmlDocument class has neither a .Close() method nor does it
implement IDisposable, is there any point in even setting it to null...?

Setting a variable to null wouldn't call Dispose anyway. It's almost
always a mistake to set a variable to null after you've finished using
it.
E.g.

XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load("<filespec>");
// retrieve an XmlNode object and change its InnerText property
xmlDoc.Save("<filespec>");

At this point, would the line below be of any benefit...?

xmlDoc = null;

Almost certainly not. The only situations in which it would be useful
are:

1) It's a member/static variable, and it's no longer useful even though
the rest of the instance might be. If it's a member variable, I'd have
another look at the design - usually I find that the whole of an object
is useful while any of it is.

2) It's used in a loop and after a certain number of iterations (before
the end) you know it won't be used any more. The JIT won't be able to
spot that you're not going to use it again, so it can't be garbage
collected until the loop finishes.

Both of these situations are rare - I can't remember the last time I
wrote a line like the above after the last use of a variable.
 
M

Mark Rae

Setting a variable to null wouldn't call Dispose anyway.

Quite so - I apologise if I implied that it would...
It's almost always a mistake to set a variable to null after you've
finished using it.

Why? I appreciate that it won't do any good, but what actual harm could it
possibly do...?
Both of these situations are rare - I can't remember the last time I
wrote a line like the above after the last use of a variable.

Me neither, hence the question...

Thanks for the clarification.
 
J

Jon Skeet [C# MVP]

Mark Rae said:
Quite so - I apologise if I implied that it would...


Why? I appreciate that it won't do any good, but what actual harm could it
possibly do...?

The same sort of harm as there'd be setting it to null a hundred times
in a hundred consecutive lines of code (but to a lesser extent): it
reduces the code readability for no gain. In particular, it implies
that there *is* a good reason to set it to null, when in fact there
isn't.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top