Laszlo
Laszlo

Reputation: 362

Is the .NET IList<> (generic IList interface) obsolete or not practical?

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<>?

Upvotes: 0

Views: 612

Answers (2)

dav_i
dav_i

Reputation: 28107

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.

Upvotes: 3

recursive
recursive

Reputation: 86094

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.

Upvotes: 0

Related Questions