views:

46

answers:

3

Should I return from model to view an IList<T> object or a List<T>? I would iterate over the Model collection and show the results in an ASP.NET MVC 2 view.

A: 

Hey,

Either is fine; IList can help for mocking if you are going to test your views, but it really doesn't matter...

Brian
sorry but what's the difference beetween IEnumerable and IList? Why should I use the first?
Stefano
IList has a `Count` property. There are plenty of LINQ operations that return an IEnumerable but not an IList, so if you want to pass the results of those into something that requires IList you have to make the list (`.ToList()`) first.
Rup
As was mentioned, IEnumerable give you the foreach ability; IList can be nice because you can access Count and also access elements by an index value (LINQ does have ElementAt, but if the source is not a list, it iterates through each element to the specified index, and can degrade performance, which I found out first hand).
Brian
thank you all!!!
Stefano
+1  A: 

If all you want to do with the list is foreach, you should return an IEnumerable<T>.

SLaks
+5  A: 

I like to keep things as Generic as possible to help with Mocking during Testing later.

If you're simply iterating over the collection for a foreach loop, I would probably use IEnumerable<T> since you don't need any IList<T> functionality.

If you do need functionality from IList<T> (like iterating over a collection in a for loop using an index), then go ahead and use IList<T>.

There's really no need to pass a concrete class to a View, so I would never use List<T>.

Justin Niessner