Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Differences between Array.Length and Array.Count() [duplicate]

Tags:

arrays

c#

Possible Duplicate:
count vs length vs size in a collection
Array.Length vs Array.Count

I declared this array:

int[] misInts = new Int[someNumber];

/* make some happy operations with the elements in misInts */

So I can get the value of SomeNumber with: misInts.Length or misInts.Count()

Arrays in C# inherit from IEnumerable. So if I have:

Func<int> misIntsF = Enumerable.Range(0, someNumber).Select(c=> /* make some happy operations that return Integers */);

I am told that if I make misIntsF.Count() I actually execute the code in the Lambda expression, get the results and count them. But the array misInts doesn't have a Lambda expressión.

Is misInts.Count() more memory consuming than misInts.Length? What are the differences between misInts.Count() and misInts.Length?

like image 566
Broken_Window Avatar asked Dec 03 '12 14:12

Broken_Window


People also ask

What is the difference between length and count?

Length is a property returning the number of elements in an Array . Count() is a LINQ extension that does the same on an IEnumerable . Optionally, it can take a predicate as parameter, and will return the number of elements that satisfy it.

What is the difference between the length field for an array and the size() method for an ArrayList?

ArrayList doesn't have length() method, the size() method of ArrayList provides the number of objects available in the collection. Array has length property which provides the length or capacity of the Array. It is the total space allocated during the initialization of the array.

Does array use length or size?

Array has length property which provides the length of the Array or Array object. It is the total space allocated in memory during the initialization of the array. Array is static so when we create an array of size n then n blocks are created of array type and JVM initializes every block by default value.

What is array length?

Basically, the length of an array is the total number of the elements which is contained by all the dimensions of that array. Syntax: public int Length { get; } Property Value: This property returns the total number of elements in all the dimensions of the Array.


2 Answers

array.Count() is actually a call to the Enumerable.Count<T>(IEnumerable<T>) extension method.

Since this method takes an IEnumerable<T> (as opposed to ICollection<T>, which has a Count property), it needs to loop through the entire sequence to figure out how big it is.

However, it actually checks whether the parameter implements ICollection<T> (which arrays do), and, if so, returns Count directly.
Therefore, calling .Count() on an array isn't much slower than .Length, although it will involve an extra typecast.

like image 54
SLaks Avatar answered Oct 25 '22 01:10

SLaks


There is no great difference since Enumerable.Count looks first if it's castable to ICollection<T>.

MSDN:

If the type of source implements ICollection, that implementation is used to obtain the count of elements. Otherwise, this method determines the count.

Source:

ICollection<TSource> collection = source as ICollection<TSource>;
if (collection != null)
{
    return collection.Count;
}

otherwise it will enumerate the sequence to count it:

int num = 0;
using (IEnumerator<TSource> enumerator = source.GetEnumerator())
{
    while (enumerator.MoveNext())
    {
        num++;
    }
}
return num;

(source: ILSpy)

like image 11
Tim Schmelter Avatar answered Oct 25 '22 01:10

Tim Schmelter