Why does IList <T> not provide all the methods that List <T> does? What should i use?
I've always been taught that programming against an interface is better, so I would set the parameters for my methods to IList<T> , not List<T> ..
But this means that I should use for List<T> only to use some methods, for example, there is Find .
Why is this? Should I continue to program against interfaces, but keep pouring or returning?
I'm a little confused why Find (for example) is not available in IList<T> , which inherits List<T> .
Personally, I would use IList<T> rather than List<T> , but then use LINQ ( Select , Where , etc.) instead of methods specific to List.
Listing in List<T> primarily eliminates much of the use of IList<T> - and actually makes it more dangerous, since the implementation may be something other than List<T> at runtime.
In the case of lists, you can continue programming against interfaces and use LINQ to filter your objects. You can even work with IEnumerable<T> , which is even higher in the hierarchy of objects.
But in general, if the consumer of your API needs to call a specific method, you probably haven’t chosen the appropriate interface for expansion.
I am a bit confused why (for example) it is not available on the IList from which the List is inherited.
While I am not involved in the decision-making process by designers, there are a few things that they probably thought.
1) Without putting these methods on IList, it preserves the purpose of the contract more clearly. According to MSDN, IList "Represents a collection of objects that can be individually accessed by index." Adding Find will change the contract for a searchable, indexable collection.
2) Each method that you enter into the interface complicates the implementation of the interface. If all of these methods were on IList, it would be much tedious to implement IList. Moreover, that:
3) Most implementations of these methods will be the same. Find, and some of the others on the list are best suited to the helper class. Take, for example, ReadOnlyCollection, Collection, ObservableCollection and ReadOnlyObservableCollection. If I had to implement Find on all of these (pre-LINQs), I would make a helper class that takes an IEnumerable and a predicate and just loops over the collections, and the implementation uses the helper method.
4) LINQ (Not such a reason why this did not happen, more than why it is not needed in the future.) Using LINQ and extension methods, all IEnumerable now "have" Find as extension method (only they called it "Where").
I think this is because IList can be different types of collection (i.e. IEnumerable of some type, array or so).
You can use the Where extension method from System.Linq. Avoid returning to the list from IList.
If you find that the IList<T> parameter passed between different classes is constantly being converted to List<T> , this indicates a fundamental problem with your design.
From what you are describing, it is clear that you want to use polymorphism, but the ongoing processing in List<T> means that IList<T> does not have the required level of polymorphism.
On the other side of the coin, you can simply aim for the wrong polymorphic method (for example, Find , not FirstOrDefault ).
In any case, you should look at your design and see what exactly you want to accomplish, and choose List<T> or IList<T> based on actual requirements, rather than style matching .
If you expose your method with the parameter IList <>, someone can pass, for example, ReadOnlyCollection <>, the witch is IList <> but not List <>. This way your API will crash at runtime. If you publish a public method with the parameter IList <>, you cannot assume that this is a specific implementation of IList <>. You should use it as an IList <> nothing more.
If the list is part of the Api or service that is being exposed, then it is probably best to have IList as the permission to allow internal implementation.
There is already a discussion on this topic.
No, in this case it makes no sense to program interfaces, because your list is NOT an IList, but it has additional methods.