我从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
{
}
Run Code Online (Sandbox Code Playgroud)
我收到以下错误
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'
Run Code Online (Sandbox Code Playgroud)
关于如何迭代这个IEnumerator的任何建议?
你通常不想迭代IEnumerator; 你只是IEnumerable直接迭代:
foreach (var pair in Concur_Dictionary) …
Run Code Online (Sandbox Code Playgroud)
如果由于某种原因,您无法访问,则IEnumerable可以执行以下操作:
while (Ien.MoveNext())
{
var pair = Ien.Current;
…
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1562 次 |
| 最近记录: |