Reputation: 13308
What's the difference between "Length", "Count()" and "Rank" for a .NET array?
Upvotes: 54
Views: 51779
Reputation: 2802
Length is the size of the array.
Count() is the amount of items in the array (from system.linq)
Rank returns the number of dimensions (a[][] = 2, a[] = 1).
Upvotes: 0
Reputation: 12381
Length
is the property of an array object and using it is the most effective way to determine the count of elements in the array (Array.Length in MSDN documentation).
Count()
is a LINQ extension method that does effectively the same. It applies to arrays because arrays are enumerable objects. It's preferred to use Length
, because Count()
is likely to be more expensive (see this question for further discussion and MSDN documentation on Count for reference).
Rank
is the property that returns the number of dimensions (a different thing entirely). When you declare an array int[,] myArray = new int[5,10];
, the Rank
of it will be 2, but it will hold a total of 50 elements (MSDN on Rank property).
Upvotes: 65
Reputation: 210402
Length
: Total number of elements in an arrayLongLength
: Same as Length
, but returned as long
(in case it's >= 231)Count()
: LINQ extension method that works with other collection types as wellRank
: Number of dimensions in array (always 1
for vectors). Only in .NET 3.5+.GetLength()
, GetLongLength()
: Length of a certain dimension of an arrayGetLowerBound()
: Starting index of a certain dimension of an array; always 0
for vectorsGetUpperBound()
: Ending index of a certain dimension of an array; always Length - 1
for vectorsInterestingly, there's no GetLongUpperBound()
or GetLongLowerBound()
...
Now that we're on the topic, what is the difference between an array and a vector in .NET?
Vectors are what you normally call "1D" arrays in C#. However, 1-dimensional arrays are actually not of a type like int[]
, but they're of the type int[*]
. C# doesn't directly support them; however, they can be created with Array.CreateInstance
, and can have non-zero lower bounds. They are, however, slightly slower than vectors, because vectors are directly supported in the CLR. Because 1-dimensional arrays are actually rarely used, C# has decided not to support them (although it can use them through the use of the var
keyword, from another module which declares them!).
Upvotes: 25
Reputation: 43523
Length
is a property of Array
since C# 1.x
Count()
is an extension method of IEnumerable<T>
, because now every T[]
implements IEnumerable<T>
implicitly.
Note that for an array, Count()
is usually much slower than Length
, because accessing Length
property is O(1), while Count
is for IEnumerable<T>
, so the program needs to go through the collection and get its count, that is O(n).
Rank
gives the demensions of the array.
Upvotes: 1
Reputation: 23054
Well .Count()
is for IEnumerable
, List
or ArrayList
types. While Length
is for Array
.
Rank
is to denote the number of dimensions of an array.
Upvotes: 1
Reputation: 37945
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.Rank
is a property returning the number of dimensions in an Array
.Upvotes: 2