Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/285.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 将项添加到字典正在中断foreach循环_C#_Loops_Dictionary_Foreach_Ui Automation - Fatal编程技术网

C# 将项添加到字典正在中断foreach循环

C# 将项添加到字典正在中断foreach循环,c#,loops,dictionary,foreach,ui-automation,C#,Loops,Dictionary,Foreach,Ui Automation,我正在尝试循环一组项,并将它们的属性添加到字典中,但由于某些原因,它并没有完成循环 我的代码: Dictionary<string, Tuple<string, string>> elementList = new Dictionary<string, Tuple<string, string>>(); public void AddElementList(AutomationElementCollection collection) {

我正在尝试循环一组项,并将它们的属性添加到字典中,但由于某些原因,它并没有完成循环

我的代码:

Dictionary<string, Tuple<string, string>> elementList = new Dictionary<string, Tuple<string, string>>(); 
public void AddElementList(AutomationElementCollection collection)
{

    foreach (AutomationElement ele in collection)
    {
        elementList.Add(ele.Cached.Name, new Tuple<string, string>(ele.Cached.LocalizedControlType.ToString(), ele.Cached.AutomationId.ToString()));
        i++;

    }
    MessageBox.Show(i.ToString());
}    
Dictionary elementList=new Dictionary();
公共无效AddElementList(AutomationElementCollection集合)
{
foreach(集合中的AutomationElement元素)
{
添加(ele.Cached.Name,新元组(ele.Cached.LocalizedControlType.ToString(),ele.Cached.AutomationId.ToString());
i++;
}
Show(i.ToString());
}    

它无法到达MessageBox。根本无法显示。通常有52个项目,循环8个项目,然后退出

你说它经过8次,然后停止。在这种情况下,当您调用字典时,它可能会抛出一个错误,因为字典中已经存在具有相同键的元素


发生这种情况的原因是,当您在字典中查找一个值时,它会查找该键,当然,如果有两个相同的键,您将不知道返回哪个值。

在调试器中捕获或中断异常,您可能会看到哪里出了错。可能是重复的密钥试图插入。我最好的猜测是
ele.Cached.Name
重复,引发了一个异常。是的,完全正确,我不敢相信我没有想到这一点!既然您的
元组
只包含两个值,为什么不使用
键值对
?Net对它的支持要比Tuple好得多。我已经编辑了你的标题。请参阅“”,其中的共识是“不,他们不应该”。