Dictionary的Collection was modified; enumeration operation may not execute.错误处理。
关于为何报这个错误,dudu解释的灰常清晰了,请参阅:http://www.cnblogs.com/dudu/archive/2006/07/31/464391.html
dudu 对只是查询Dictionary给出了办法,而我需要对Dictionary进行删除操作,我的办法是先把键拿出,在对Dictionary操作,做了两次循环。
如:
List<string> removeKeyList = new List<string>(); foreach (KeyValuePair<string, MobileUserInfo> user in UserList) { if (user.Value.UserTime < DateTime.Now.AddMinutes(0 - getUserTimeOut())) { removeKeyList.Add(user.Key); } } //为了避开Err:Collection was modified; enumeration operation may not execute.的错误 for (int i = 0; i < removeKeyList.Count; i++) { UserList.Remove(removeKeyList[i]); }