dictionary 的 reset 方法

//在对 Dictionary 作 foreach 操作的时候,不可以作 Remove() 操作。
public Dictionary<string,int> stateItem;

private void resetState()
    {
        Dictionary<string,int>.KeyCollection itemColl = stateItem.Keys;
        string[] itemList = new string[itemColl.Count];
        itemColl.CopyTo(itemList, 0);
       foreach (string item in itemList)
        {
            stateItem.Remove(item);
            stateItem.Add(item, 0);
        }
    }

posted @ 2008-01-19 12:15  老大卫  阅读(185)  评论(0编辑  收藏  举报