Dictionary.ElementAt方法在某些类中可见,但不是其他类 [英] Dictionary.ElementAt method is visible in some classes, but not others

查看:341
本文介绍了Dictionary.ElementAt方法在某些类中可见,但不是其他类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个字典,其元素需要遍历并进行更改。我不能使用foreach语句,因为它有时会抛出InvalidOperationException,表示该集合在枚举期间无法修改。



我可以使用一个for循环和Dictionary。 ElementAt方法,我成功地在其他类中使用它,但在这个特定的类中,找不到方法ElementAt!任何想法?

解决方案

ElementAt 是一个扩展方法 System.Linq.Enumerable 中定义。



您需要添加一个using子句以使其可见:

 使用System.Linq; 

请注意,DictionaryAt on Dictionary< TKey,TValue>尽管如此,由于字典的实现不能保证元素具有任何特定的顺序,也不会对字典进行更改,顺序也不会改变。


I have a Dictionary whose elements I need to iterate through and make changes. I cannot use foreach statement, since it sometimes throws InvalidOperationException, saying that the collection cannot be modified during an enumaration.

I can use a for loop, in conjunction with Dictionary.ElementAt method, and I successfully used it in other classes, but in this particular class, the method ElementAt cannot be found! Any ideas?

解决方案

ElementAt is an extension method defined in System.Linq.Enumerable.

You need to add a using clause to make it visible:

using System.Linq;

Note that ElementAt on a Dictionary<TKey,TValue> doesn't make a lot of sense, though, as the dictionary implementation does not guarantee elements to be in any specific order, nor that the order does not change if you make changes to the dictionary.

这篇关于Dictionary.ElementAt方法在某些类中可见,但不是其他类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆