


























最近在对博客园的程序进行性能优化,在优化过程中用到了Dictionary
,在通过foreach将Dictionary中的数据写入数据库时,遇到了这样的错误:Dictionary<int, int> _dictionary = new Dictionary<int, in>();
//添加数据操作省略
foreach(KeyValuePair<int, int> item in _dictionary)
{
}
那什么会产生这样的异常信息呢?
foreach实际上执行的代码是:
Dictionary<int, int>.Enumerator enumerator = _dictionary.GetEnumerator();
try
{
while (enumerator.MoveNext())
{
}
}
finally
{
IDisposable d = enumerator as IDisposable;
if (d != null) d.Dispose();
}
if (this.version != this.dictionary.version)
{
ThrowHelper.ThrowInvalidOperationException(ExceptionResource.InvalidOperation_EnumFailedVersion);
}
而异常就是在这里发生的,因为Add操作时改变了Dictionary的version,通过查看Insert(TKey key, TValue value, bool add)的源代码会看出。
我觉得Dictionary<TKey, TValue>应该提供一个方法,可以设置在MoveNext时是否进行版本检查,因为有时在foreach操作时,可能并不关心Dictionary中的数据是否被修改,我遇到的就是这样的情况,现在由于这个问题而不能使用foreach,而只能采取其他方法遍历Dictionary<TKey, TValue>中的数据。
我采用的方法是:
Dictionary<int, int> _dictionary = new Dictionary<int, in>();
//添加数据操作省略
int[] dataArray = new int[_dictionary.Values.Count];
_ dictionary.Values.CopyTo(evcArray, 0)
for (int i = 0; i < dataArray.Length; i++)
{
}
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。