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.
views:
46answers:
3
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
2010-10-26 15:44:22
sorry but what's the difference beetween IEnumerable and IList? Why should I use the first?
Stefano
2010-10-26 15:50:29
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
2010-10-26 16:04:58
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
2010-10-26 16:07:21
thank you all!!!
Stefano
2010-10-27 16:15:47
+1
A:
If all you want to do with the list is foreach
, you should return an IEnumerable<T>
.
SLaks
2010-10-26 15:46:05
+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
2010-10-26 15:46:39