Reputation: 17233
I got an IEnumerator type from a ConcurrentDictionary . I am trying to iterate through it using the following way
System.Collections.Generic.IEnumerator<System.Collections.Generic.KeyValuePair<string,string>>
Ien = Concur_Dictionary.GetEnumerator();
foreach (KeyValuePair<string, string> pair in Ien) -->Error here
{
}
I get the following error
Error 4 foreach statement cannot operate on variables of type 'System.Collections.Generic.IEnumerator<System.Collections.Generic.KeyValuePair<string,string>>' because 'System.Collections.Generic.IEnumerator<System.Collections.Generic.KeyValuePair<string,string>>' does not contain a public definition for 'GetEnumerator'
Any suggestions on how I could iterate through this IEnumerator ?
Upvotes: 0
Views: 1839
Reputation: 1708
You can use MoveNext()
of it.
var enumerator = getInt().GetEnumerator();
while(enumerator.MoveNext())
{
//Line of codes..
}
You can do something like this.
Or this can be done.
foreach (var item in collection)
{
// do your stuff
}
Try this thing it worked for me.
Upvotes: 1
Reputation: 185970
You usually don't want to iterate through the IEnumerator
; you just iterate over the IEnumerable
directly:
foreach (var pair in Concur_Dictionary) …
If, for some reason, you don't have access to the IEnumerable
, you can do this:
while (Ien.MoveNext())
{
var pair = Ien.Current;
…
}
Upvotes: 5