29

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.

8
  • 4
    I can't find my Lippert-whistle, so I don't think we'll get a good answer today :( Commented Aug 26, 2015 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. Commented Aug 26, 2015 at 10:47
  • 4
    I guess this former stackoverflow post has the right answer. Commented Aug 26, 2015 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. Commented Aug 26, 2015 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. Commented Aug 26, 2015 at 17:27

1 Answer 1

37

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.

7
  • 14
    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. Commented Aug 26, 2015 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. Commented Aug 26, 2015 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. Commented Aug 26, 2015 at 13:27
  • @StupidOne: If you go that route, then any array should also have a count-property. Commented Aug 26, 2015 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 Commented Aug 26, 2015 at 14:57

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.