Reputation: 541
If I want to take a List as an argument of a method, Resharper gives a hint that using the IList interface might be a better idea. If I then replace List with IList, Resharper suggests IEnumerable. In Java I would also take the List interface and not ArrayList as an argument. But is it really good looking code to use iEnumerable as an argument? Is this good practice to use IEnumerable if no functionality of IList or List are required?
Upvotes: 8
Views: 6582
Reputation: 4772
Here is the article that made me change my mind about all that and made me start following the principle of
take the most generic type, return the most specific type
So to answer your question
Is this good practice to use IEnumerable if no functionality of IList or List are required?
yes, it is
note: do not over do it ! I've seen code that accepted an IEnumerable
, then threw an exception if the parameter wasn't a List
. This is stupid. What the sentence I quoted above means is "don't ask for more specificity than you need"
Upvotes: 15