C# 固定大小列表
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1625302/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Fixed Size to List
提问by user196546
For declaration perspective the following is allowed
对于声明的观点,允许以下内容
IList<string> list= new string[3];
list.Add("Apple");
list.Add("Manago");
list.Add("Grapes");
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 ?
1)它编译得很好,但运行时我收到"Collection was of fixed size"
错误。当然,集合是按大小动态增长的,为什么这样的声明会被编译器接受?
2) What are the different lists that i can assign to IList ? Example
2) 我可以分配给 IList 的不同列表是什么?例子
IList<string> fruits=new List<string>();
Here I am assigning List to IList ,What are the various collection classes can i assign to IList?
在这里,我将 List 分配给 IList ,我可以将哪些集合类分配给 IList?
采纳答案by JaredPar
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>
。甲System.Array
类型有固定尺寸,其不能被改变。Add 方法IList<T>
旨在向底层集合添加一个新元素并将其大小增加 1。这对 a 来说是不可能的System.Array
,因此它会抛出。
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>
.
这里System.Array
真正想要实现的是只读样式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>
.
至于可以赋值给什么类型的问题IList<T>
,其实有很多,包括:ReadOnlyCollection<T>
和Collection<T>
。列表太长,无法放在这里。查看这一切的最佳方法是IList<T>
在反射器中打开并查找IList<T>
.
回答by Jake Pearson
When you call list.Add, you are trying to insert an item to the end of you array. Arrays are a fixed size collection so you can't do an Add. Instead you will have to assign the entries via the indexer:
当您调用 list.Add 时,您正在尝试将一个项目插入到数组的末尾。数组是固定大小的集合,因此您不能执行添加操作。相反,您必须通过索引器分配条目:
list[0] = "a";
list[1] = "b";
list[2] = "c";
回答by Michael Bray
@Jake: Actually a string[] IS castable to an IList... but you can't ADD to it.
@Jake:实际上 string[] 可以转换为 IList ......但你不能添加它。
If you want to pre-populate the list, then you coulduse something like:
如果要预先填充列表,则可以使用以下内容:
IList<string> list= new string[3] { "Apple", "Mango", "Grapes" };
but then what's the point of making it an IList? You still couldn't add to it. If it really is a fixed-sized list, then make it a string[]. Otherwise, make it a List(), as Jake suggests.
但是,使它成为 IList 有什么意义呢?你仍然无法添加它。如果它确实是一个固定大小的列表,则将其设为 string[]。否则,按照 Jake 的建议,将其设为 List()。