It depends on how the class writer (whether Microsoft or yourself) implemented the IEnumerable and IEnumerator interfaces.
I suspect that some implementations simply use an indexer (essentially equivalent to a for(;{} call), while others need to traverse several internal datastructures. If you have some really time-critical code, it would be best to run a test yourself (load 100,000 items into your collection/class) and see which is faster for you.
Myself, I prefer the foreach(){} call because it makes for less code, which means higher quality (less code means fewer things to go wrong) and easier maintenance (intuitively obvious as to what's going on).
Chip H.
If you want to get the best response to a question, please check out FAQ222-2244 first
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.