ASP.NET MVC 2 Controller To View: IList or List?
Should I return from model to view an IList<T>
object or a List<T>
? I would iterate ove开发者_如何学Pythonr the Model collection and show the results in an ASP.NET MVC 2 view.
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>
.
If all you want to do with the list is foreach
, you should return an IEnumerable<T>
.
Either is fine; IList can help for mocking if you are going to test your views, but it really doesn't matter...
精彩评论