我从ConcurrentDictionary那里得到了一个IEnumerator类型。我正在尝试使用以下方式遍历它
System.Collections.Generic.IEnumerator<System.Collections.Generic.KeyValuePair<string,string>>
Ien = Concur_Dictionary.GetEnumerator();
foreach (KeyValuePair<string, string> pair in Ien) -->Error here
{
}我得到以下错误
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'对如何遍历这个IEnumerator有什么建议吗?
发布于 2013-02-20 16:20:38
您通常不希望遍历IEnumerator;您只需直接遍历IEnumerable:
foreach (var pair in Concur_Dictionary) …如果由于某种原因,您没有访问IEnumerable的权限,您可以这样做:
while (Ien.MoveNext())
{
var pair = Ien.Current;
…
}发布于 2013-02-20 16:32:01
你可以使用它的MoveNext()。
var enumerator = getInt().GetEnumerator();
while(enumerator.MoveNext())
{
//Line of codes..
}你可以做这样的事情。
或者可以这样做。
foreach (var item in collection)
{
// do your stuff
}试试这玩意儿它对我很管用。
https://stackoverflow.com/questions/14975144
复制相似问题