Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cast IList to List

I am trying to cast IList type to List type but I am getting error every time.

List<SubProduct> subProducts= Model.subproduct; 

Model.subproduct returns IList<SubProduct>.

like image 891
Pankaj Avatar asked Feb 05 '10 13:02

Pankaj


People also ask

Is IList faster than List?

Results. IList<T> uses 40 Bytes more than List<T> .

What is the difference between IList and List?

The main difference between List and IList in C# is that List is a class that represents a list of objects which can be accessed by index while IList is an interface that represents a collection of objects which can be accessed by index.

How do I initialize an IList?

IList<string> strings = new List<string>(); The preceeding line of code will work, but you will only have the members of IList available to you instead of the full set from whatever class you initialize.


2 Answers

Try

List<SubProduct> subProducts = new List<SubProduct>(Model.subproduct); 

or

List<SubProduct> subProducts = Model.subproducts as List<SubProduct>; 
like image 138
Pbirkoff Avatar answered Sep 30 '22 05:09

Pbirkoff


How about this:

List<SubProduct> subProducts = Model.subproduct.ToList(); 
like image 20
Mark Seemann Avatar answered Sep 30 '22 06:09

Mark Seemann