When planning out my programs, I often start with a chain of thought like so:
A football team is just a list of football players. Therefore, I should represent it with:
var football_team = new List<FootballPlayer>();
The ordering of this list represent the order in which the players are listed in the roster.
But I realize later that teams also have other properties, besides the mere list of players, that must be recorded. For example, the running total of scores this season, the current budget, the uniform colors, a string
representing the name of the team, etc..
So then I think:
Okay, a football team is just like a list of players, but additionally, it has a name (a
string
) and a running total of scores (anint
). .NET does not provide a class for storing football teams, so I will make my own class. The most similar and relevant existing structure isList<FootballPlayer>
, so I will inherit from it:class FootballTeam : List<FootballPlayer> { public string TeamName; public int RunningTotal }
But it turns out that a guideline says you shouldn't inherit from List<T>
. I'm thoroughly confused by this guideline in two respects.
Why not?
Apparently List
is somehow optimized for performance. How so? What performance problems will I cause if I extend List
? What exactly will break?
Another reason I've seen is that List
is provided by Microsoft, and I have no control over it, so I cannot change it later, after exposing a "public API". But I struggle to understand this. What is a public API and why should I care? If my current project does not and is not likely to ever have this public API, can I safely ignore this guideline? If I do inherit from List
and it turns out I need a public API, what difficulties will I have?
Why does it even matter? A list is a list. What could possibly change? What could I possibly want to change?
And lastly, if Microsoft did not want me to inherit from List
, why didn't they make the class sealed
?
What else am I supposed to use?
Apparently, for custom collections, Microsoft has provided a Collection
class which should be extended instead of List
. But this class is very bare, and does not have many useful things, such as AddRange
, for instance. jvitor83's answer provides a performance rationale for that particular method, but how is a slow AddRange
not better than no AddRange
?
Inheriting from Collection
is way more work than inheriting from List
, and I see no benefit. Surely Microsoft wouldn't tell me to do extra work for no reason, so I can't help feeling like I am somehow misunderstanding something, and inheriting Collection
is actually not the right solution for my problem.
I've seen suggestions such as implementing IList
. Just no. This is dozens of lines of boilerplate code which gains me nothing.
Lastly, some suggest wrapping the List
in something:
class FootballTeam
{
public List<FootballPlayer> Players;
}
There are two problems with this:
It makes my code needlessly verbose. I must now call
my_team.Players.Count
instead of justmy_team.Count
. Thankfully, with C# I can define indexers to make indexing transparent, and forward all the methods of the internalList
... But that's a lot of code! What do I get for all that work?It just plain doesn't make any sense. A football team doesn't "have" a list of players. It is the list of players. You don't say "John McFootballer has joined SomeTeam's players". You say "John has joined SomeTeam". You don't add a letter to "a string's characters", you add a letter to a string. You don't add a book to a library's books, you add a book to a library.
I realize that what happens "under the hood" can be said to be "adding X to Y's internal list", but this seems like a very counter-intuitive way of thinking about the world.
My question (summarized)
What is the correct C# way of representing a data structure, which, "logically" (that is to say, "to the human mind") is just a list
of things
with a few bells and whistles?
Is inheriting from List<T>
always unacceptable? When is it acceptable? Why/why not? What must a programmer consider, when deciding whether to inherit from List<T>
or not?
List
?" I would consider the "list of players" to be a most fundamental property of a football team - above all others like name or budget. A team can exist which has no name (neighborhood kids who form two teams to play on an afternoon) but a team without players does not make sense to me. As for being surprised, I don't know. I'm having trouble imagining anyone being surprised by such inheritance, but I feel I might be overlooking something. – Magdalenamagdalenemy_team.CountFootballs
,my_team.CountSpareUniforms
... – StoneSquash()
should just be a virtual method on theRectangle
class, and you get a helpfulNotSupportedException
when you try to call the method on aSquare
that is masquerading as aRectangle
. To avoid that exception, I would then add a virtualIsSquashable
property to the class, thus converting the vexing exception into a boneheaded exception (since they should have just checked the flag!) Then, I... oh no, I've gone cross-eyed. OO design is hard. – Toursstring
is required to do everything anobject
can do and more. – DermatoRectangle
s andSquare
s. My purpose in using that particular example was not to say that in every possible class structure aSquare
should be implemented as a subclass of aRectangle
, just to give a simple example to illustrate the general concept of inheritance without getting too bogged down in extended explanations and/or obscure terminology. – DesignedlyList<T>
that makes sense would be if its namedFootballPlayerCollection
(purists would correct me thatCollection<T>
is a better choice, ok, but you get the point). – AstoundingList<T>
is not a good fit because it can contain duplicates. Could a football team really have the same player more than once?! That would be the case ifFootballTeam
inherited from or contained aList<T>
. I recommend that you look at a collection type with stricter guarantees, such asISet<T>
. – Fortenberrystruct
is defined and then goes against those recommendations. Excellent question with some good points. But, a Football Team is not just a list of players. It has a roster, employees, expenses, records, etc. It's not as simple as you made it to be. ;) – SouterYou don't say "John McFootballer has joined SomeTeam's players". You say "John has joined SomeTeam"
you could however still sayJohn McFootballer has joined SomeTeam's roster
(which is nearly identical to the first statement). – Monachism