473,657 Members | 2,489 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Count or Length why have both?

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 =^.^=
Dec 6 '07 #1
5 1827
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.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard. caspershouse.co m

"colin" <co*********@nt world.NOSPAM.co mwrote in message
news:cu******** *********@newsf e6-gui.ntli.net...
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 =^.^=

Dec 6 '07 #2
Hi,


--
Ignacio Machin
http://www.laceupsolutions.com
Mobile & warehouse Solutions.
"colin" <co*********@nt world.NOSPAM.co mwrote in message
news:cu******** *********@newsf e6-gui.ntli.net...
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.
Dec 6 '07 #3


"colin" <co*********@nt world.NOSPAM.co mwrote in message
news:cu******** *********@newsf e6-gui.ntli.net...
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
Dec 6 '07 #4
"Mythran" <ki********@hot mail.comwrote in message
news:9F******** *************** ***********@mic rosoft.com...
>

"colin" <co*********@nt world.NOSPAM.co mwrote in message
news:cu******** *********@newsf e6-gui.ntli.net...
>some enumerable types like collections use Count but
but other enumerable types like arrays use length
why the difference?
.....
>
<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 =^.^=
Dec 6 '07 #5
Hi,
--
Ignacio Machin
http://www.laceupsolutions.com
Mobile & warehouse Solutions.
"colin" <co*********@nt world.NOSPAM.co mwrote in message
news:FL******** *********@newsf e6-gui.ntli.net...
"Mythran" <ki********@hot mail.comwrote in message
news:9F******** *************** ***********@mic rosoft.com...
>>

"colin" <co*********@nt world.NOSPAM.co mwrote in message
news:cu******* **********@news fe6-gui.ntli.net...
>>some enumerable types like collections use Count but
but other enumerable types like arrays use length
why the difference?
....
>>
<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.
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 ", you should use List<Tinstead
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.
Dec 6 '07 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
497
by: brendonlam | last post by:
Hi there, Hoping someone could advise me here pls; I'm creating a C# class that implements a telnet client socket allowing a VB . NET application to communicate with telnet servers. After leaving the app running for just 6 hrs, the thread count exploded to close to 1000, before the app finally stops responding. The handles probably hit close to 10000. Tracing my code, I isolated the leak to when I execute a telnet command. Everytime I...
1
3142
by: JD | last post by:
Hi guys I'm trying to write a program that counts the occurrences of HTML tags in a text file. This is what I have so far: #include <stdio.h> #include <stdlib.h> #include <string.h> #define MB 1048576
9
10097
by: Jerry | last post by:
In limiting textbox input to 500 characters I would like to include a dynamic count of characters input while the user is typing into a textbox. This would obviously be a client side control, possibly a custom validator with a function written in javascript. Has anyone done this? Does someone have an example? Regards
2
1857
by: roN | last post by:
Hey, I got following: <form name="Formular" method="post" onSubmit="return chkFormular()" action="mail.php" enctype="text/plain"> .... .... .... <td align="right" valign="top" class="text"><font
68
6795
by: Martin Joergensen | last post by:
Hi, I have some files which has the following content: 0 0 0 0 0 0 0 1 1 1 1 0 0 1 1 1 1 0 0 1 1 1 1 0 0 1 1 1 1 0 0 0 0 0 0 0
24
2310
by: Derek Hart | last post by:
Is there an efficient line of code to count the number of instances of one string within another. If I have the sentence: "I want to go to the park, and then go home." It would give me a count of 2 for the word "go" Derek
1
5395
by: el jefe | last post by:
hey im new, howdy. here is my prob, i am have this problem using the string count function, i am trying to center some text. looking at my code will clarify. i will be forever in debt to whoever helps. thanks //Linemaker.h #ifndef _LM_H #define _LM_H class LineMaker { private:
7
381
by: willie | last post by:
Marc 'BlackJack' Rintsch: # Apologies if I'm being dense, but it seems
2
2742
by: kifee | last post by:
Write a program which print your complete name and your city name at the middle two rows of the screen with the following attributes. Use white back ground and blue foreground color if the length of the name is greater than the length of the city name. Use Black background and red foreground color if the length of the city name is greater than the length of your name. Note: Use the strlen function to calculate the length of both strings.
0
8392
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8305
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8823
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8603
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7320
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5632
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4151
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4301
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
1604
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.