>Hmm, good question. I'm not sure that fundamentally there is any difference in your two snippets. Since it's possible that the code in the loop could change either this.TreeView1.Nodes.Count or nCount, then the question really boils down to whether the value is re-evaluated for each loop, or if it's fixed at the start of the first loop. I don't know the answer to that off the top of my head. > >It's probably infinitesimally quicker to use nCount instead of this.TreeView1.Nodes.Count, assuming there are enough loops to offset the overhead of declaring and initializing the extra variable. There could be a large speed gain if TreeView1 is an ActiveX; my understanding is that COM calls like that are expensive and that your second snippet is best practice in these cases. > >UPDATE: Docs for the C# FOR command: > >"The for loop executes a statement or a block of statements repeatedly until a specified expression evaluates to false." > >Seems to me, this means the logical expression is evaluated before each loop, not just once.
It is not COM but a standard .NET 2.0 TreeView. As I suppose it is evaluated every time it should be faster on the second example (variable). Just wanted to confirm.
"Once again, we come to the Holiday Season, a deeply religious time that each of us observes, in his own way, by going to the mall of his choice." -- Dave Barry