Cannot apply indexing with [] to an expression of type 'System.Collections.Generic.IEnumerable<>

后端 未结 7 1176
长情又很酷
长情又很酷 2020-12-14 05:41

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 i

7条回答
  •  余生分开走
    2020-12-14 06:03

    One reason can be that the IEnumerable may contain an unknown number of items. Some implementations produce the list of items as you iterate over it (see yield for samples). That does not work very well with accessing items using an index. which would require you to know that there are at least that many items in the list.

提交回复
热议问题