Count or Length why have both?

  • Thread starter Thread starter colin
  • Start date Start date
C

colin

some enumerable types like collections use Count but
but other enumerable types like arrays use length
why the difference?

Im not sure what my function is going to be passed yet,
some enumerable type such as an array or a list,
but I need to allocate space first, so I need to know
how big it is, so I need to use ICollection as a parameter type.

luckily arrays have ICollection as a base too, so why do arrays have
length as a field too? If I try to access Count of an array
such as "int[] a;" I have to cast it to ICollection first ..
do I get a different result?

I sometimes find I need to use a different
collection class and have to change Count to Length or vica versa.

Colin =^.^=
 
Colin,

I don't know that there is a good reason. Perhaps Count on the public
interface of an array didn't make sense for some reason. Perhaps length
makes more sense with an array because of convention as opposed to count,
which applies more to sets (I don't know, I never thought of an array in
terms of a set, even though it definitely is).

However, it should matter little if you are going to take an instance of
ICollection into your routine. If that is the case, you will ^always^ use
the Count property on the ICollection implementation.

And yes, the array will always return Length through the Count property.
 
Hi,




--
Ignacio Machin
http://www.laceupsolutions.com
Mobile & warehouse Solutions.
colin said:
some enumerable types like collections use Count but
but other enumerable types like arrays use length
why the difference?

I think that Count is for variable length collections, wheater Length is for
fixed length collections.
 
colin said:
some enumerable types like collections use Count but
but other enumerable types like arrays use length
why the difference?

Im not sure what my function is going to be passed yet,
some enumerable type such as an array or a list,
but I need to allocate space first, so I need to know
how big it is, so I need to use ICollection as a parameter type.

luckily arrays have ICollection as a base too, so why do arrays have
length as a field too? If I try to access Count of an array
such as "int[] a;" I have to cast it to ICollection first ..
do I get a different result?

I sometimes find I need to use a different
collection class and have to change Count to Length or vica versa.

Colin =^.^=

IMO: Length should have been reserved for the length of a 2+ dimensional
item. The Count would return the number of items in an array, stack, or
other collection. Count, in all practical purpose really is a count. Why
is length involved?!?

But, being that other languages used length prior to .Net coming into
existence, maybe it's just a borrowed concept (for instance, java[script]
uses a length property on arrays to return the number of elements in the
array).

<shrug> I do think Ignacio hit the nail on the head though (at least it
makes sense to me), Count is for variable length collections, L/length is
for fixed length collections (for .Net).

Mythran
 
Mythran said:
.....

<shrug> I do think Ignacio hit the nail on the head though (at least it
makes sense to me), Count is for variable length collections, L/length is
for fixed length collections (for .Net).

Mythran

That would make sense if fixed sized collections had Length
reporting the amount of storage space and then also had Count
as the extent of the filled locations, but that isnt the case.

such as autosizing arrays wich is basicalay what im creating.
unfortunatly ArrayList isnt generic,
but that actually uses Count and Capacity.

String has Length rather than count, although as there was strlen()
since forever this is kinda mandatory.

I still dont quite get why Array.Count isnt accessable directly
when you can access it by casting it to ICollection.

Colin =^.^=
 
Hi,


--
Ignacio Machin
http://www.laceupsolutions.com
Mobile & warehouse Solutions.
colin said:
That would make sense if fixed sized collections had Length
reporting the amount of storage space and then also had Count
as the extent of the filled locations, but that isnt the case.

There is no way to know the amount of storage (nor need to know it in the
first place)
such as autosizing arrays wich is basicalay what im creating.
unfortunatly ArrayList isnt generic,
but that actually uses Count and Capacity.

ArrayList is kind of "deprecated" said:
String has Length rather than count, although as there was strlen()
since forever this is kinda mandatory.

String is inmutable, hence the use of Length.
strlen() is a function, it has no place in this discussion.
I still dont quite get why Array.Count isnt accessable directly
when you can access it by casting it to ICollection.

For the reasons explained above, Array is a fixed collection therefore you
have Length.
When you cast it to ICollection though you are treating it like a variable
length collection so you get your Count.

I do not see what is the problem there.
 
Back
Top