27

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?

6
  • 4
    stackoverflow.com/questions/1506963/array-length-vs-array-count stackoverflow.com/questions/300522/… Commented Dec 3, 2012 at 14:42
  • @Kiyura your linked questions talk about terminology. This question asks about performance. Commented Dec 3, 2012 at 14:43
  • (Bob Ross) - "make some happy operations with the elements in misInts" Commented Dec 3, 2012 at 14:48
  • You're right Rotem, I'm concerned about memory comsumption and performance because this is part of a larger algorithm with loops inside loops inside loops everywhere. Commented Dec 3, 2012 at 14:50
  • @Rotem, this is why it's a comment and not an answer, and also why I didn't say "duplicate," it was only meant to be a pointer to information OP might not have seen yet. Commented Dec 3, 2012 at 15:03

2 Answers 2

35

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.

Sign up to request clarification or add additional context in comments.

3 Comments

I came across this answer looking for the difference between Count and Length in PowerShell. In PowerShell, Count should always be used instead of Length because even though Count and Length produce the same result for arrays, collection objects don't return the expected result for Length. For example, ([ordered]@{'a' = 0; 'bc' = 0;'def' = 0;}).Keys.Length returns 1,2,3 and not 3. This is because Length returns a list of Length properties for each key, which is the length of each string.
@DaveF you should make your comment into an Answer, because you have the best answer here! What you mention can definitely bite someone if they are not paying close attention.
@(([ordered]@{'a' = 0; 'bc' = 0;'def' = 0;}).Keys).Length returns 3
11

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)

4 Comments

Please not: Although you quote ILSpy as your source, this actually is not just an implementation detail, it indeed is documented.
@DanielHilgarth: I've also linked MSDN, but since the source is quite readable in this case i have shown that instead. However, edited my answer to also quote msdn to make it clear.
I know that you linked it. I justed wanted to emphasise the fact that it is not an implementation detail as I just had this exact discussion a few days back with someone else. And the way your answer is structured it looks like you infered this info only from the source code and not from the documentation.
@DanielHilgarth: Edited my answer to make it more clear. But since Slaks has already answered it theoretically i just wanted to add something new instead of just quoting MSDN or repeat his answer in other words. Linq often appears magical for people, so it might help to see the code behind.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.