Tales from a Trading Desk

Noise from an Investment Bank

.NET 1.1 C# for vs foreach

Most of us .NET engineers know that for is faster than foreach - here and here. Its thus easy to re-code a foreach to use a for when iterating over a list, string etc.  However, if you want to access all the keys/values in a hashtable, it appears as though you are still forced to you foreach, or am I missing something ?
Posted: Feb 25 2004, 12:06 AM by mdavey | with 2 comment(s)
Filed under:


Mike said:

How about using .GetEnumerator()?
# February 24, 2004 9:27 PM

Jerry Pisk said:

IEnumerable.GetEnumerator is exactly what foreach does.

But I'm curious - how did you come to the conclusion that for is faster than foreach? For strings that might be true, but for generic IEnumerable objects (let's say ones that also implement ICollection and IList, so you can actually write a for loop on them)?
# February 25, 2004 12:15 AM