C# 为什么 ICollection 索引在实例化时不起作用?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1875655/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 21:34:02  来源:igfitidea点击:

Why ICollection index does not work when instantiated?

c#listindexingicollection

提问by demokritos

When we declare a parameter as ICollection and instantiated the object as List, why we can't retrive the indexes? i.e.

当我们将参数声明为 ICollection 并将对象实例化为 List 时,为什么我们无法检索索引?IE

ICollection<ProductDTO> Products = new List<ProductDTO>();
Products.Add(new ProductDTO(1,"Pen"));
Products.Add(new ProductDTO(2,"Notebook"));

Then, this will not work:

然后,这将不起作用:

ProductDTO product = (ProductDTO)Products[0];

What is the bit I am missing?
[Yes, we can use List as declaration an it can work, but I don't want to declare as list, like:

我缺少什么?
[是的,我们可以使用 List 作为声明它可以工作,但我不想声明为列表,例如:

List<ProductDTO> Products = new List<ProductDTO>();

]

]

采纳答案by Chris W. Rea

The ICollectioninterface doesn't declare an indexer, so you can't use indexing to fetch elements through a reference of that type.

ICollection的接口不声明索引器,这样你就不能使用索引通过该类型的引用来获取元素。

You could perhaps try IList, which adds some more functionality, while still being abstract. Of course, this may impact other design decisions so I would look at this carefully.

您或许可以尝试IList,它增加了一些功能,同时仍然是抽象的。当然,这可能会影响其他设计决策,所以我会仔细研究一下。

回答by ChaosPandion

ICollection does not define an indexer.

ICollection 没有定义索引器。

ICollection Non-Generic

ICollection 非泛型

ICollection Generic

ICollection 通用

回答by Stephen M. Redd

The basic problem is that ICollection doesn't define an index. For the List this is done by the implementation of IList.

基本问题是 ICollection 没有定义索引。对于 List,这是通过 IList 的实现来完成的。

Try this:

尝试这个:

IList<ProductDTO> Products = new List<ProductDTO>(); 

Alternatly, you can keep using ICollection and convert to an array when you need to access the elements by the index:

或者,当您需要通过索引访问元素时,您可以继续使用 ICollection 并转换为数组:

ICollection<ProductDTO> Products = new List<ProductDTO>();        
ProductDTO z = Products.ToArray()[0];

回答by Cecil Has a Name

Then this will work:

然后这将起作用:

ProductDTO product = ((IList<ProductDTO>)Products)[0];

The reason is that the compiler evaluates the lvalue, that is the variable on the left side of '=', to find out which methods and properties it knowsit can access at compile-time. This is known as static typing, and ensures that an object member can be accessed directly at runtime by statically knowing that the member is always reachable.

原因是编译器评估lvalue,即 '=' 左侧的变量,以找出它知道它可以在编译时访问哪些方法和属性。这称为静态类型,并通过静态地知道该成员始终可访问来确保可以在运行时直接访问对象成员。

回答by jeremcc

Using LINQ, you can do this:

使用 LINQ,您可以这样做:

ProductDTO product = (ProductDTO)Products.ElementAt(0);

回答by NoRelect

Using Linq, you can access an element in an ICollection<> at a specific index like this:

使用 Linq,您可以访问 ICollection<> 中特定索引处的元素,如下所示:

myICollection.AsEnumerable().ElementAt(myIndex);

回答by Qais Bsharat

You can also use extension methods to convert it to (list, array) of ProductDTO

您还可以使用扩展方法将其转换为ProductDTO 的(list, array)

ICollection<ProductDTO> Products = new List<ProductDTO>();
var productsList = Products.ToList();

Then you can use it like that:

然后你可以这样使用它:

productsList[index]