> [...]
> 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
> 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.
>
[quoted text clipped - 6 lines]
>
> 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.

Signature
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Mark Rae - 16 Jun 2007 20:03 GMT
> 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.

Signature
http://www.markrae.net
Jon Skeet [C# MVP] - 17 Jun 2007 08:22 GMT
> > Setting a variable to null wouldn't call Dispose anyway.
>
[quoted text clipped - 5 lines]
> 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.

Signature
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too