Collection was modified; enumeration operation may not execute. when update values of hashtable

前端 未结 1 1036
面向向阳花
面向向阳花 2021-01-28 18:35

this code throw exception while i am trying to update value ,first value only updated and then throw the exception \"Collection was modified; enumeration operation may not exec

1条回答
  •  鱼传尺愫
    2021-01-28 18:57

    You need to make copy of hh.Keys, because you are trying to change Hashtable which is invalid operation while enumerating its keys in the foreach loop.

    Try this code

    foreach (string dd in new List(hh.Keys.Cast()))
    {
        hh[dd] = "some_value";
        MessageBox.Show(dd);
    }
    
        

    0 讨论(0)
    提交回复
    热议问题