3

Can anyone explains me why to not inherit from IList? I saw many inputs here that it's better to inherit from Collection if i need to provide custom collection class for my object and not from IList. Why is that? Why people saying not inherit from IList? If possible please make an example.

i see its possible to like this so why not use it as List provide more features:

class FootballTeam : List<FootballPlayer> 
{ 
}
Arie
  • 3,041
  • 7
  • 32
  • 63
  • If you need some custom functionality then you need to implement from IList interface and in these case you must implement all the methods of the interface but in the provide code snippet you are just adding two properties to the List so both the implementations are different according to the requirement. Inherit from IList for adding two properties to collection is not worth for implementation. – Niranjan Singh Jan 19 '17 at 02:44
  • **[Guidelines for Collections](https://msdn.microsoft.com/en-us/library/dn169389(v=vs.110).aspx)** – Ňɏssa Pøngjǣrdenlarp Jan 19 '17 at 02:50
  • See [Why not inherit from List?](http://stackoverflow.com/q/21692193/719186) – LarsTech Jan 19 '17 at 04:00

3 Answers3

6

overview of collection interface.

This doesn't provide direct answer, might have overview

IList provides you the flexibility of implement this functionality.

public interface IList<T> : ICollection<T>, IEnumerable<T>, IEnumerable
{
    T this[int index] { get; set; }
 
    int IndexOf(T item);
    void Insert(int index, T item);
    void RemoveAt(int index);
}

if you use class FootballTeam : List<FootballPlayer> you can extent the properties in your FootballTeam class.

Ideally it will be necessary when you want to extend your collection. Suppose you have to implement AddSort(T item) then class FootballTeam : List<FootballPlayer>

if you use class FootballTeam : ILIst<FootballPlayer> you might be reinventing the wheel that Add(), and Contains() should be implemented by yourself

I would recommend to use Composition over inheritance

class FootballTeam : //So here can use any team base class or anything necessary
{ 
  public string TeamName; 
  public int RunningTotal 
  
  private List<FootballPlayer> _players
  public IEnumerable<FootballPlayer> Players {get;set;}
}

Update I have updated the Players as IEnumerable so will have much flexibility.

illustration and Credits from this link: When To Use IEnumerable, ICollection, IList And List

Eldho
  • 7,795
  • 5
  • 40
  • 77
  • Two more questions: 1) What about if i would like to use Dictionary? also approach as a property in class? 2) So if you say it's better to use Composition but then in which situation its better to create additional collection class and inherit either from List or Collection rather than composition? – Arie Jan 19 '17 at 10:38
  • The composition over inheritance is based on the requirement. Think about scenario you want a collection that you need a particular logic then you need to extend (implement) List in your class. for normal cases like representing over a class it better to use composition – Eldho Jan 19 '17 at 12:11
  • @Arie i have updated information, i think it will solve your question on the comment – Eldho Jan 19 '17 at 12:19
  • so let's say i want particural logic and change something in Add/Remove of IList methods so then i could make additional collection class which would inherit from List and in Add/Remove method add some additional code and if i just want to use Add/Remove etc as it is then i could use composite am i right what i just wrote? – Arie Jan 19 '17 at 15:10
  • if you want to override on you derived class you can inherit List and provide a new keyword on the implementation. See this for more info http://stackoverflow.com/questions/580202/how-do-i-override-listts-add- method-in-c also http://stackoverflow.com/questions/22165015/how-to-override-list-add-method – Eldho Jan 20 '17 at 05:07
  • 1
    Great answer, even better when giving credit to the author of the graphic used in this post. https://www.claudiobernasconi.ch/2013/07/22/when-to-use-ienumerable-icollection-ilist-and-list/ – Claudio Bernasconi Nov 21 '22 at 10:01
  • @updated credits – Eldho Nov 21 '22 at 10:15
3

better not to do it all and model it like

class FootballTeam
{ 
    public string TeamName; 
    public int RunningTotal 
    public IList<FootballPlayer> Players
}

you'd normally inherit off a collection if you wanted to implement a new kind of collection like a DiskCachedCollection or something

This is known as "Composition over Inheritance" https://en.wikipedia.org/wiki/Composition_over_inheritance

It becomes obvious why when a new requirement comes a long.... "We want to track the management staff against a team". Suddenly you go hmmm, how can a team be a collection of players and a collection of management. Oh! it's not, a team is composed of players and management

Keith Nicholas
  • 43,549
  • 15
  • 93
  • 156
-1

If you are going to customize either List or Collection interface which its concrete implementation List or Collection does not provide then do implement the IList or ICollection inteface , other wise to use any list or collection "Composition is better than inheritence".

TsunamiCoder
  • 120
  • 1
  • 12