My understanding is that when I loop through a vector, I do so with length
, but I see that they also use the Count()
.
What is it for and what is the difference?
For example:
for(int i = 0; i < vector.Count(); i++) {
}
My understanding is that when I loop through a vector, I do so with length
, but I see that they also use the Count()
.
What is it for and what is the difference?
For example:
for(int i = 0; i < vector.Count(); i++) {
}
If by vector you mean a
Array
.Length
is a propertyArray<T>
that refers to the length of the array, that is, for how many objects of the typeT
the memory has been separated..Count()
is a LINQ extension method ofIEnumerable<T>
(Array implementsIEnumerable<T>
)It is a method that gets the number of elements depending on the underlying type in various ways, usually by getting an enumerator and iterating to the end of
IEnumerable<T>
(There are exceptions)This form is equivalent to calling
To know which of the two should be used in which case let's see what the compiler generates.
For example for the following code:
The compiler generates the following code
CIL
in LINQPad:As you can see, both
count1
andcount2
are initialized with the result of calling theSystem.Linq.Enumerable.Count<Int32>
. This shows that the expressions are equivalent.However if we use
.Length
Generate the following code:
In which we don't even see the call to
.Length
but it already exists inCIL
an operationldlen
to access get the number of elements of an array, which is an optimization.In the case of
List<T>
List does not have a property
Length
but it does have a propertyCount
in addition to the extension methodCount()
.For the following code
The following is generated:
Something interesting happens here, as we can see in the first call to the property
Count
is accessed ( ) while the call to the extension method works the same as in the case of which makes a method callgetter
Count
get_Count
Count()
Array<T>
System.Linq.Enumerable.Count<Int32>
In general, as can be seen when you have the property available
Length
orCount
in the case ofList<T>
using it, since it will be the fastest way to access the elements. If there is no such property then useCount()
which works for anyIEnumerable<T>
Length()
tends to refer to contiguous elements - a string(String
) has a length, for example.Count()
tends to refer to the number of elements in a collection.Size()
tends to refer to the size of the collection, often this can be different from length(length
) in cases like vectors (or strings), there can be 10 characters in a string, but storage is reserved for 20. It can also refer to a number of items.Capacity()
is used to refer specifically to the space allocated in the collection and not to the number of valid elements in the collection. If a data type has capacity x and size y defined, then size usually refers to the number of actual elements.I think the main point is human language and idioms, the size of a string doesn't seem very obvious, while the length of a set is equally confusing, although it could be used to refer to the same thing (number of elements) in a collection of data.
I assume that when you say
vector
you are referring to oneList<T>
or aArray
.The answer is that there is no difference, since they both get the number of elements in a sequence. In the case of a
Array
, you can use the methodCount()
since it's a Linq extension method and it's an iterable object as well.On these pages you can find the official documentation for the property
Length
and the methodCount()
(in English).When using collections as lists or vectors, the method to get the size of the collection is Count(). On the other hand, when we handle arrays or text strings, the method used is Length(), it simply depends on whether it is a collection or an array, but both return the size.