A
Anthony Bouch
Everything I know about looping structures says to be careful about
expressions that need to be evaluated again and again for each
test/increment of a loop.
I came across this piece of code the other day and stopped to think for a
minute about whether it was right or not.
foreach (XmlNode node in doc.SelectNodes("/root/map/")
{
//Do something
}
Am I missing something here or is this going to perform the Xpath select for
every iteration? Or is it the case that once the iterator has been retrieved
by the foreach statement that it will use the same one for each loop without
evaluating the SelectNodes statement over and over again.
If I was able to read IL - I would look at what was going on here myself.
I've been trained to think about doing this the following way.
XmlNodeList list = doc.SelectNodes("/root/map/");
foreach (XmlNode node in list)
{
//Do something
}
Enlightenment appreciated....
expressions that need to be evaluated again and again for each
test/increment of a loop.
I came across this piece of code the other day and stopped to think for a
minute about whether it was right or not.
foreach (XmlNode node in doc.SelectNodes("/root/map/")
{
//Do something
}
Am I missing something here or is this going to perform the Xpath select for
every iteration? Or is it the case that once the iterator has been retrieved
by the foreach statement that it will use the same one for each loop without
evaluating the SelectNodes statement over and over again.
If I was able to read IL - I would look at what was going on here myself.
I've been trained to think about doing this the following way.
XmlNodeList list = doc.SelectNodes("/root/map/");
foreach (XmlNode node in list)
{
//Do something
}
Enlightenment appreciated....