23

In C# for example, arrays have Length property. But other collection types like lists, etc. have Count property. Is there a reason why these two are different? If so I would like to know.

closed as primarily opinion-based by gbjbaanb, user40980, durron597, gnat, user53019 Aug 26 '15 at 17:25

Many good questions generate some degree of opinion based on expert experience, but answers to this question will tend to be almost entirely based on opinions, rather than facts, references, or specific expertise. If this question can be reworded to fit the rules in the help center, please edit the question.

  • 4
    I can't find my Lippert-whistle, so I don't think we'll get a good answer today :( – MetaFight Aug 26 '15 at 9:48
  • 4
    Just a wild guess, as I have no inside knowledge on how the CLR was designed: the details of how arrays work were specified before the collection types. Calling the property Length is the most natural name for it, and as there wasn't a preexisting standard to conform to, so this is what the designer of arrays chose to use. Then the collections were specified afterwards, but Length isn't appropriate for some collections (it implies linearity, so for unordered collections it is not a reasonable name) so Count was chosen as more logically consistent. – Jules Aug 26 '15 at 10:47
  • 4
    I guess this former stackoverflow post has the right answer. – Doc Brown Aug 26 '15 at 11:27
  • 6
    @MetaFight: I recently recorded a series of educational videos and at one point I mention that I have no idea why the designers used both length and count. It has always struck me as bizarre. The comment from Jules above seems plausible. – Eric Lippert Aug 26 '15 at 16:55
  • 3
    Meta note - I have cast the 5th VTC as I don't believe this question can be definitively answered. The existing answer is a solid and plausible answer, but it is not backed with evidence. Likewise, Lippert's comment leads me to think that no one knows the answer as it could have been due to an oversight as opposed to a conscious decision. – user53019 Aug 26 '15 at 17:27
30

They are named differently because semantically they are quite different:

A collection's Count is the number of items currently stored in it and can potentially change over time.

An array's Length is the maximum number of items it can hold (it will have a length of 10 even if you haven't stored that many items in it) and is immutable.

Example:

If I have a bucket that can fit a maximum of 100 balls in it it has a Length of 100. If I put 50 balls into it it then it has a Count of 50.

If I add 10 more balls the Count becomes 60 but the Length is still 100. In order to change the Length I need to get a different bucket.

Array probably uses the word Length because under the hood it's allocating a contiguous block (a length) of memory based on the capacity multiplied by the item size. Although the fact that the List class uses "Capacity" for a similar (though mutable) concept suggests array may use the word "Length" for historical reasons.

  • 12
    A T[] with a length of N always stores exactly N values of type T. Semantically, not all of those values may be meaningful (they might be null for example), but they exist. This is different from the usual meaning of capacity (as used by List<T> for example). You're right that Count can change while Length can't. Then again, nothing mandates that Count will, in fact, change. It is also used for immutable collections. – user7043 Aug 26 '15 at 11:41
  • @delnan oh dear. didn't realise the word Capacity was already used in C# like this. I've accidentally overloaded it. Thanks for pointing this out. I'll update my answer to clarify. – combinatorics Aug 26 '15 at 11:47
  • Difference between Capacity and Length is - Capacity might change over the life-cycle of the object, while Length always stays the same. If I see a Length property on the object, I'd assume it's "hard" maximum number (or the border / index), while if I see Capacity property, I'd assume it's "soft" maximum number which I should only check against if I'm concerned with performance. – StupidOne Aug 26 '15 at 13:27
  • @StupidOne: If you go that route, then any array should also have a count-property. – Deduplicator Aug 26 '15 at 13:49
  • 1
    Damn StringBuilder... in stricter Languages like Vigil the StringBuilder class would have been properly punished for breaking convention github.com/munificent/vigil – Falco Aug 26 '15 at 14:57

Not the answer you're looking for? Browse other questions tagged or ask your own question.