views:

26

answers:

1

This line of code confuses me:

List<string> keys = new List<string>();
IDictionaryEnumerator ca = cache.GetEnumerator();
while (ca.MoveNext())
{
  keys.Add(ca.Key.ToString());
}

What is an Enumerator? Is it connected to Enumerations? I try to find a tutorial on enumerators, but without success. Hope someone will have patience to explain it to me.

+1  A: 

You don't show what type 'cache' actually is but your code is equivalent to :

foreach(var ca in cache)  
{
   keys.Add(ca.Key.ToString();
}

foreach() uses the Enumerator as well, but cleaner through compiler-generated code.

Henk Holterman
I see. Which one is faster? Do you know that?
askmo
@askmo: If you have to ask, use neither. But these 2 are the same.
Henk Holterman