Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / June 2007

Tip: Looking for answers? Try searching our database.

XmlDocument, close and dispose

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Mark Rae - 16 Jun 2007 17:33 GMT
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

Signature

http://www.markrae.net

Peter Duniho - 16 Jun 2007 18:43 GMT
> [...]
> 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
Jon Skeet [C# MVP] - 16 Jun 2007 19:14 GMT
> 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


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.