为什么数组实现IList? [英] Why array implements IList?

查看:113
本文介绍了为什么数组实现IList?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请参阅定义的的System.Array 的类

public abstract class Array : IList, ...

从理论上讲,我应该能够编写此位和快乐

Theoretically, I should be able to write this bit and be happy

int[] list = new int[] {};
IList iList = (IList)list;

我也应该能够从IList中

I also should be able to call any method from the iList

 ilist.Add(1); //exception here

我的问题是不是为什么我得到一个异常,而是为什么数组实现IList

推荐答案

由于数组可以通过索引快速访问,而的IList / 的IList< T> 是是支持这一点的唯一集合接口。因此,也许你真正的问题是:为什么会出现与索引不断收集没有接口?并且,我没有答案。

Because an array allows fast access by index, and IList/IList<T> is are the only collection interfaces that support this. So perhaps your real question is "Why is there no interface for constant collections with indexers?" And to that I have no answer.

有对集合没有只读接口两种。而且我错过了那些甚至比恒定的大小更多的索引器的界面。

There are no readonly interfaces for collections either. And I'm missing those even more than a constant sized with indexers interface.

IMO应该有几个根据集合的特征(通用)集合接口。而名字应该是不同的太列表的东西有一个索引是非常愚蠢的海事组织。

IMO there should be several more (generic) collection interfaces depending on the features of a collection. And the names should have been different too, List for something with an indexer is really stupid IMO.


  • 只需枚举的IEnumerable&LT; T&GT;

  • 只读,但没有索引(.Count之间,。载,...)

  • 可调整大小,但没有索引,即设置像(添加,删除,...)电流的ICollection&LT; T&GT;

  • 只读与索引(索引,的indexOf,...)

  • 与索引(索引与一个setter)常量大小

  • 可变大小索引(插入,...)电流的IList&LT; T&GT;

  • Just Enumeration IEnumerable<T>
  • Readonly but no indexer (.Count, .Contains,...)
  • Resizable but no indexer, i.e. set like (Add, Remove,...) current ICollection<T>
  • Readonly with indexer (indexer, indexof,...)
  • Constant size with indexer (indexer with a setter)
  • Variable size with indexer (Insert,...) current IList<T>

我认为目前的集合接口都是不好的设计。但由于他们有属性告诉你哪些方法是有效的(这是对这些方法的合同的一部分),它不会破坏替代原则。

I think the current collection interfaces are bad design. But since they have properties telling you which methods are valid(and this is part of the contract of these methods) it doesn't break the substitution principle.

这篇关于为什么数组实现IList?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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