Possible Duplicate:
count vs length vs size in a collection
In .NET, pretty much all collections have the .Count
property.
Sometimes I wonder if it would be better to have it on Array
as well, directly though, not through ICollection
.
It's just something you make an exception in your mind only for arrays.
So is it better to be "more correct" or "more uniform" in this case?
The count of an array can be found using the length property, also known as the JavaScript Array Length. The length property can do multiple jobs, including returning or setting the number of elements.
The length property sets or returns the number of items in an array. The value of a length property is the integer with a positive sign and a value less than 2 to the 32nd power. To set the length of the array, use the array.length =number syntax. The length property returns a Number, representing the number of elements in the array object.
If there are more than 2,147,483,647 elements in an array, the LongLength property is handy. Moreover, Count is an alias for the Length property. You can also use Count to find the length of an array. It is easy to find the number of items stored in an array in PowerShell.
If there are more than 2,147,483,647 elements in an array, the LongLength property is handy. Moreover, Count is an alias for the Length property. You can also use Count to find the length of an array.
In many cases where a one-dimensional array is used, it's essentially being used as a fixed-size list.
Personally I will often declare an array as IList<T>, and use the Count property rather than Length:
IList<string> strings = new string[] { ...};
Another useful member of IList<T> that does not exist in Array is the Contains() method.
If you're using C# 3.0, you may use Enumerable.Count() extension method that works on all IEnumerable implementations, including lists, arrays and dictionaries.
It causes some overhead, but it's usually tolerable.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With