1

Where and Why should I implement an IList? Do anybody need to implement an IList when actually there are large number of extension methods and utilities that enrich Generic List already?? Could you please guide me, I myself cant imagine a use case that requires implementing an IList!


Update

For example suppose I need a cash list. In this case I simply Create a CashList that extends List class. I dot not have to implement even a single method and can manage cash behavior inside CashList and also use several methods of List. Why should I implement the IList instead of inheriting from List ?

public class CashList : List<CashItem>
{
    public void UpdateItems();
    public void SetExpireTime();

}
a.toraby
  • 3,232
  • 5
  • 41
  • 73
  • 7
    Well if you want to implement your own custom collection. – Rahul Oct 07 '15 at 11:18
  • 1
    Why vote down? I think this is a very clear and straightforward question. I need to know Where Should I implement an IList? – a.toraby Oct 07 '15 at 11:18
  • 6
    `IList` is just a generic definition (read by index, add items, etc..). Now `List` uses an array as backing storage, but feel free to make a tree or a linked list (for performance, storage reasons). Have a look at the neighbours (https://docs.oracle.com/javase/7/docs/api/java/util/List.html). – Caramiriel Oct 07 '15 at 11:19
  • It can be an option if you wish to implement a custom collection with standard R/W methods and a predefined type (if you use the non generic interface). Custom collections can be very useful and handy, you should search on this (broader) topic instead of "IList" which is more restrictive. – AFract Oct 07 '15 at 11:19
  • 3
    @a.toraby > agree with you about the downvote. This is the SO plague. I personally upvoted your topic. – AFract Oct 07 '15 at 11:20
  • 4
    Possible duplicate of [When to use IList and when to use List](http://stackoverflow.com/questions/17170/when-to-use-ilist-and-when-to-use-list) – Mukesh Kumar Oct 07 '15 at 11:22
  • 4
    @AFract _"I personally upvoted your topic"_ Unfortunately that's the other side of the "SO plague" - why upvote a Q that has no (apparent) research behind it? – James Thorpe Oct 07 '15 at 11:23
  • @JamesThorpe > agree ;). My griefs are more against the people who downvote without explain why (most of the downvotes I think !!) rather than the downvotes themselves – AFract Oct 07 '15 at 11:25
  • 7
    List<> is a concrete implementation of IList<> but it is limited. You cannot change the way it works, none of its methods are virtual. So if you, say, want to modify the Add() method and throw an exception when there's something wrong with the item then List<> doesn't help you. You have to create your own implementation, inheriting from IList<> so it still basically behaves like a list. You'd still use List<> but you'd encapsulate it, the IList<> implementations are generally just one-liners. – Hans Passant Oct 07 '15 at 11:26
  • 2
    @a.toraby > regarding your example, the purpose can be to change the inner implementation of the container used for your collection, and/or to override methods like Add (for example to do some checks on the items before to append them to list). Edit : Hans has just replied about this, cross posted with my comment. – AFract Oct 07 '15 at 11:28
  • 4
    About your update: https://stackoverflow.com/questions/21692193/why-not-inherit-from-listt – Caramiriel Oct 07 '15 at 11:32
  • It's worth mentioning that `IList` interface is what the extension methods, utilities, UI data binding frameworks etc. are supposed to use/check for. `List` class is just one of the possible implementations of that interface. – Ivan Stoev Oct 07 '15 at 12:00

0 Answers0