473,396 Members | 2,093 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

XmlDocument, close and dispose

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
--
http://www.markrae.net

Jun 16 '07 #1
4 17798
On Sat, 16 Jun 2007 09:33:46 -0700, Mark Rae <ma**@markNOSPAMrae.net
wrote:
[...]
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
Jun 16 '07 #2
Mark Rae <ma**@markNOSPAMrae.netwrote:
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.

--
Jon Skeet - <sk***@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
Jun 16 '07 #3
"Jon Skeet [C# MVP]" <sk***@pobox.comwrote in message
news:MP*********************@msnews.microsoft.com. ..
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.
--
http://www.markrae.net

Jun 16 '07 #4
Mark Rae <ma**@markNOSPAMrae.netwrote:
"Jon Skeet [C# MVP]" <sk***@pobox.comwrote in message
news:MP*********************@msnews.microsoft.com. ..
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...?
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.

--
Jon Skeet - <sk***@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
Jun 17 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Van Steenbergen Jan | last post by:
Hi everyone, I'm making a SOAP request to a webservice from classic asp with(SOAP Toolkit v3.0). This function has to return the XML document or the xmlstring to the classic asp page. How can...
7
by: Willem van Rumpt | last post by:
Hi all, coming from an unmanaged programming background, I took my time to sort out the IDisposable and finalizer patterns. Just when I thought I had it all conceptually neatly arranged, the...
10
by: Jim H | last post by:
I sometimes get the following error from my Form's Dispose Method when the application is closing: ------------------------------------------------- An unhandled exception of type...
6
by: Simon Harris | last post by:
Hi All, Do I need to use both Conn.Close() & Conn.Dispose() when I have finished with an SQL connection? -- I am using the free version of SPAMfighter for private users. It has removed...
35
by: Eric Sabine | last post by:
In my Finally block, I was using cn.close (where cn is an ADO.NET connection object, SQLConnection to be exact) and then I came across the following in some microsoft code. If Not cn Is Nothing...
8
by: Paul W | last post by:
Hi - in an asp.net application, how should I close out a sqlclient.connection? What combination and order of Conn.close conn.dispose conn=nothing Should I use? Specifically, is the...
3
by: Joris De Groote | last post by:
Hi, I use Adobe Acrobat to read tekst from PDF files. After that the file has been read, I move the file in a folder (using the date I got from the text I got from Acrobat). Now here is my...
10
by: Atara | last post by:
Suppose I have the following functions: Public Function myF1ToStream(...) As System.IO.MemoryStream . . . Dim ms As New System.IO.MemoryStream(buf) Return ms End Function Public Function...
0
by: dthope69 | last post by:
I have a client that is requiring my web service to accept XMLDocument object as input parameter. I had created it initially to accept byte string and it worked fine. However, when changing to a...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.