Friday
Feb272009
Doncaster Meeting and Programming Puzzle
Friday, February 27, 2009 at 05:03PM Went to Doncaster for an exam board today.
The meeting room had this very impressive chandelier, which lent a lot to the proceedings.
Spent some time chatting with Colin, the external examiner, about programming matters. We were talking about good practices, properties and get/set methods and the like, and a question occurred.
“Why do some types in C# have a Length property (for example String and Array) whereas others have a Count method (for example List)? Both of them do the same thing, so it seems to be silly to have different names for them.”
Colin and I think we know the answer. But do you?
Rob |
15 Comments | 
Reader Comments (15)
http://stackoverflow.com/questions/300522/count-vs-length-vs-size-in-a-collection
Also, how long is a string? :P
Arrays and Strings are as long as the 'length' method says they are, but lists are not.
Arrays and Strings are as long as the 'length' method says they are, but lists are not.
Arrays and Strings are as long as the 'length' method says they are, but lists are not.
Arrays and Strings are as long as the 'length' method says they are, but lists are not.
Arrays and Strings are as long as the 'length' method says they are, but lists are not.
Arrays and Strings are as long as the 'length' method says they are, but lists are not.
On the other hand collection classes like Lists can have things added and removed at any time, so when you need to know how many they contain you have to go and count the items. This means that some code needs to run, and the designers of the classes indicate this by making the Count() behaviour a method, so that you know that some algorithm is running to work out the answer, rather than a simple look up.
I now have a rule when creating properties that if you aren't just looking up an answer you should use a method instead, so that the user of your classs knows when they are running code inside your object.
I may be wrong.
Cool info either way :D