Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why ICollection index does not work when instantiated?

Tags:

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

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<ProductDTO> Products = new List<ProductDTO>();

]

like image 789
demokritos Avatar asked Dec 09 '09 17:12

demokritos


1 Answers

Using LINQ, you can do this:

ProductDTO product = (ProductDTO)Products.ElementAt(0); 
like image 94
jeremcc Avatar answered Oct 06 '22 09:10

jeremcc