tags:

views:

212

answers:

3

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?

+1  A: 

See the answers to the following related question

JaredPar
Thanks, haven't seen that one.
Joan Venge
@Downvoter, really a -1 for this one? Care to explain
JaredPar
+2  A: 

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.

Joe
+1  A: 

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.

elder_george