固定大小列出 [英] Fixed Size to List

查看:98
本文介绍了固定大小列出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有关以下是允许的声明角度

For declaration perspective the following is allowed

    IList<string> list= new string[3];
    list.Add("Apple");
    list.Add("Manago");
    list.Add("Grapes");



1)
它编译罚款,但运行时我得到 收藏是固定大小的错误。
Ofcourse,集合动态地大小,为什么这样的声明是由编译器接受成长?

1) It compiles fine,But runtime i am getting "Collection was of fixed size" error. Ofcourse ,collection is dynamically grown by size,why did such declaration is accepted by complier ?

2)什么是我可以分配到IList不同列表?
示例

2) What are the different lists that i can assign to IList ? Example

IList<string> fruits=new List<string>();

下面我分配表到IList,有哪些不同的集合
类可我分配到IList?

Here I am assigning List to IList ,What are the various collection classes can i assign to IList?

推荐答案

这里的根本问题是的System.Array 违反了替代通过实施原则的IList< T> 。 A 的System.Array 类型都有它不能改变一个固定的大小。在的IList< Add方法; T> 旨在将新元素添加到底层的收集,它的大小1。这是不可能的的System.Array ,因此它抛出。

The underlying problem here is that System.Array violates the substitution principle by implementing IList<T>. A System.Array type has a fixed size which cannot be changed. The Add method on IList<T> is intended to add a new element to the underlying collection and grow it's size by 1. This is not possible for a System.Array and hence it throws.

什么的System.Array 真的想在这里实现是一个只读式的的IList< T> 。遗憾的是没有这种类型的框架存在,因此它实现退而求其次:的IList< T>

What System.Array really wants to implement here is a read only style IList<T>. Unfortunately no such type exists in the framework and hence it implements the next best thing: IList<T>.

至于什么类型分配给的IList<的问题; T> ,其实有不少,包括: ReadOnlyCollection还< T> 收藏< T> 。这份名单是太长,放在这里。看到这一切,最好的办法是打开的IList< T> 反射器并查找派生类型的IList<的; T>

As to the question about what types are assignable to IList<T>, there are actually quite a few including: ReadOnlyCollection<T> and Collection<T>. The list is too long to put here. The best way to see it all is to open IList<T> in reflector and look for derived types of IList<T>.

这篇关于固定大小列出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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