-1

如果有人能够提供帮助,谢谢,它只会在崩溃后运行,我不知道如何修复它谢谢

{
    //DOES NOT WORK YET
    foreach (Client c in records)
        if (nameBox.Text != c.Name)
        {
          records.Add(c);
        }
        else
        {
          MessageBox.Show("Member Already Exists");
        }           
}
4

1 回答 1

2

您似乎正在尝试修改您正在迭代的同一个集合,这是不允许的。虽然尚不清楚您实际上要做什么,但您可能需要检查以下内容:

var clientName = nameBox.Text.Trim(); // after validating that it's not null or empty
if (!records.Any(c => string.Equals(c.Name, clientName, StringComparison.InvariantCultureIgnoreCase)))
{
     records.Add(new Client { Name = clientName } );
}
else
{
     MessageBox.Show("Member already exists");
}
于 2013-09-08T00:54:15.517 回答