Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Iterating with Enumerator

Status
Not open for further replies.

vladibo

Programmer
Sep 14, 2003
161
CA
Is iterating with Enumerator more efficient than the iterating with for loop?

Thanks in advance
 
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
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top