不能申请用[]索引到类型的表达式“System.Collections.Generic.IEnumerable<> [英] Cannot apply indexing with [] to an expression of type 'System.Collections.Generic.IEnumerable<>

查看:185
本文介绍了不能申请用[]索引到类型的表达式“System.Collections.Generic.IEnumerable<>的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有为什么索引中没有IEnumerable的允许的任何具体原因。

Is there any specific reason why indexing is not allowed in IEnumerable.

我发现我有问题的解决办法,但只是好奇,想知道为什么它不允许索引。

I found a workaround for the problem I had, but just curious to know why it does not allow indexing.

谢谢,

推荐答案

由于它不是。

索引由的IList 覆盖。 的IEnumerable 表示我有一些的IList的权力,但不是所有的人。

Indexing is covered by IList. IEnumerable means "I have some of the powers of IList, but not all of them."

某些集合(像链表),不能以实际的方式进行索引。但是,他们可以访问的项目,由项目。 的IEnumerable 适用于这样的集合。需要注意的是集合可以实现两者的IList&放大器; IEnumerable的(和许多其他)。您通常只能找到的IEnumerable 作为函数参数,这意味着该函数可以接受任何类型的集合,因为它需要的是最简单的访问模式。

Some collections (like a linked list), cannot be indexed in a practical way. But they can be accessed item-by-item. IEnumerable is intended for collections like that. Note that a collection can implement both IList & IEnumerable (and many others). You generally only find IEnumerable as a function parameter, meaning the function can accept any kind of collection, because all it needs is the simplest access mode.

这篇关于不能申请用[]索引到类型的表达式“System.Collections.Generic.IEnumerable<>的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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