0

Well I came across a lot of legacy code that uses IList<> in the function signatures but inside works with List<>.

Now only List<> and arrays (int[] implements IList<>) implement IList<>. ArrayList implements the non generic IList and since generics were introduced there is no point in using boxable/unboxable collections.

List<> down is still has an array implementation. I do not think is there a point in using arrays in C# anymore. LinkedList<> does not implements IList<>

And if I think back to the C++ STL containers/collections they did not had interfaces too. From design perspective there was no issue in what to use or make it interchangeable. Or you used an iterator, if you wanted interchangeable, here in C# you can use IEnumerable<> if you want and can be flexible and lazy.

I see no reason to change it to an array and I do not think I can write a better collection class than List<>.

Now out of pragmatism how bad would it be if I would replace all the IList<> with List<>?

Laszlo
  • 97
  • 8
  • 3
    Show some code. As-is, your question doesn't mean much. Code that uses interfaces i.e. `IList` is more flexible than code that requires a specific implementation i.e. `List`. So why would you switch from the more flexible design the less flexible one? Without a good code example, it's not possible to understand your scenario. See http://stackoverflow.com/help/mcve – Peter Duniho Nov 30 '14 at 20:18
  • Might you want to use `ReadOnlyCollection`? What harm do you believe it's doing to keep the generality? – Jon Skeet Nov 30 '14 at 20:18
  • Lots of discussion over the years. Not a new question so I won't repeat history. Some prior discussions here http://stackoverflow.com/questions/434761/array-versus-listt-when-to-use-which. I would agree that ArrayList (but not IList<> because of it) is no longer an optimal choice. – Trevor Ash Nov 30 '14 at 20:27
  • It's true there are only a few types that directly implement `IList` in the framework. However, one of them is `Collection` which serves as a base class for several framework collections, e.g. `ObservableCollection`, as well as for many custom collections. – Mike Zboray Nov 30 '14 at 20:31
  • OK heard all your concerns and checked again and `Collection`, `ReadOnlyCollection` and `ObservableCollection` implement `IList`. And it was asked before just could not find it. In our backend code we never used any of those so I thought to go for a more uniform approach for faster development. – Laszlo Dec 01 '14 at 09:17
  • You cannot talk about "good", "bad", or "better" without stating what characteristics you want, how you compare. Using `IList` will be better for generality (if you need it), using `List` will be (slightly) better for perfomance (if you need it). – Kris Vandermotten Jan 07 '16 at 17:23

2 Answers2

3

Now only List<> and arrays (int[] implements IList<>) implement IList<>

Not true.

For example, if you use NHibernate, it has it's own implementation of IList<T> which it uses to represent lists and if I remember rightly, will throw errors if you expose List<T> in a mapped entity.

In general though, exposing the interface, rather than the implementation in your code leaves things more flexible as someone using your code may want to use their custom implementation of IList<T> and forcing them to use List<T> when they don't have to is restrictive.


Using interfaces instead of concrete implementation is considered good practice: it's an integral part of the D in SOLID.

dav_i
  • 25,285
  • 17
  • 94
  • 128
  • 2
    Some other built-in classes that implement `IList` are `Collection`, `ReadOnlyCollection`, and `ObservableCollection`. – Tim Destan Nov 30 '14 at 20:50
  • Checked it again and yes this is true: Collection, ReadOnlyCollection, ObservableCollection implement IList. Still feels like being overly flexible with IList for things that will never happen. – Laszlo Dec 01 '14 at 09:06
  • @Laszlo Using interfaces instead of concrete implementations is *good*, which I just expanded on in my edit. – dav_i Dec 01 '14 at 09:11
  • Well I was thinking about IList. – Laszlo Dec 01 '14 at 20:03
0

People can (and do) make their own implementations of IList<T>. If you replace all occurrences of IList<T> with List<T>, then you'll break all those uses.

Linked lists can't implement it because they don't allow random access. I think IList<T> is a good interface, and use it regularly.

recursive
  • 77,417
  • 29
  • 137
  • 228
  • 1
    "Linked lists can't implement it" -- linked lists _can_ implement it, just not efficiently. So .NET's `LinkedList` doesn't. But that doesn't stop someone else from doing so. – Peter Duniho Nov 30 '14 at 22:45