Thought for the Dazed

I've had to give up that Distance Learning course as I was having trouble seeing the teacher.

Flickr
www.flickr.com
RobMiles' items Go to RobMiles' photostream
Twitter
C# Yellow Book

Search entire site
« And So to Seattle | Main | Mended »
Friday
Feb272009

Doncaster Meeting and Programming Puzzle

Went to Doncaster for an exam board today.

3314441850

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?

Reader Comments (15)

I think the length is there because of tradition (previous languages) and not any particularly good reason.
February 27, 2009 | Unregistered CommenterJeff Martin
February 27, 2009 | Unregistered CommenterJeff Martin
Length seems to make more sense. Not sure really, maybe they're showing off.

Also, how long is a string? :P
February 27, 2009 | Unregistered CommenterJoel
Because C# was designed to teach children bad programming practise. That's what Warren and Darren told me anyway.
February 27, 2009 | Unregistered CommenterPat
Personally i would think of strings and arrays as being horizontal (Robs theory on Arrays being a series of boxes to store things in), therefore length would be appropriate. Lists however imply a vertical structure therefore the counting the items within it makes more sence.
February 27, 2009 | Unregistered CommenterDavid
The List class is built using an array, the 'count' method returns how many items are actually stored in the array, but the 'capacity' method returns the actual length of the array.

Arrays and Strings are as long as the 'length' method says they are, but lists are not.
February 27, 2009 | Unregistered CommenterRobert
The List class is built using an array, the 'count' method returns how many items are actually stored in the array, but the 'capacity' method returns the actual length of the array.

Arrays and Strings are as long as the 'length' method says they are, but lists are not.
February 27, 2009 | Unregistered CommenterRobert
The List class is built using an array, the 'count' method returns how many items are actually stored in the array, but the 'capacity' method returns the actual length of the array.

Arrays and Strings are as long as the 'length' method says they are, but lists are not.
February 27, 2009 | Unregistered CommenterRobert
The List class is built using an array, the 'count' method returns how many items are actually stored in the array, but the 'capacity' method returns the actual length of the array.

Arrays and Strings are as long as the 'length' method says they are, but lists are not.
February 27, 2009 | Unregistered CommenterRobert
The List class is built using an array, the 'count' method returns how many items are actually stored in the array, but the 'capacity' method returns the actual length of the array.

Arrays and Strings are as long as the 'length' method says they are, but lists are not.
February 27, 2009 | Unregistered CommenterRobert
The List class is built using an array, the 'count' method returns how many items are actually stored in the array, but the 'capacity' method returns the actual length of the array.

Arrays and Strings are as long as the 'length' method says they are, but lists are not.
February 27, 2009 | Unregistered CommenterRobert
id take a guess that 'list' is a linked list therefore it would be impossible to know the length without iterating - 'counting' - through the list, whereas the former is an array therefore the length is already known. This is coming from a c++ programer tho.
February 28, 2009 | Unregistered Commenterarron
I think it is because the String and Array types have a particular length when they are created and this does not change. You can't adjust the length of an array, and you can't change the contents of a string, you can only make new ones. Therefore such a type can have a length property that can be looked up, i.e. there is no need to calculate anything.

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.
March 1, 2009 | Registered CommenterRob
Strings can change length, when a person uses Console.Readline, they would be dictating the size of the String, then maybe, you would change the String length later on when you change its contents.

I may be wrong.

Cool info either way :D
March 1, 2009 | Unregistered CommenterJoel
To help assist code in making it friendly to those reading it without out prior knowledge of the language of course.
March 6, 2009 | Unregistered CommenterSimon Johnson

PostPost a New Comment

Enter your information below to add a new comment.
Author Email (optional):
Author URL (optional):
Post:
 
All HTML will be escaped. Hyperlinks will be created for URLs automatically.