Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does C# array not have Count property? [duplicate]

Possible Duplicate:
count vs length vs size in a collection

Really strange:

C# arrays such as the following

double[] test = new double[1];

support the Length property to get the size of the array. But arrays also implement an IList interface:

IList<double> list = test;

However, the IList interface provides also a Count property. How come the array ("test" in this case) doesn't?

Edit: Thanks to all of you who pointed out that it is in fact the ICollection interface (not IList) which provides the Count property, and also that this is due to explicit implementation of the interface.

like image 294
Chris Avatar asked Jan 22 '11 19:01

Chris


2 Answers

Simply, they chose to call it Length, and implement Count via explicit interface implementation -something like:

int ICollection.Count { get { return Length; } }
like image 65
Marc Gravell Avatar answered Oct 12 '22 03:10

Marc Gravell


It was a design choice about Naming, not semantics.

Arrays have a Length property, as does the String.

Length signals immutable: You cannot Add to or Remove from an array.

Lists and other containers have a Count property that can usually change.

Oh, and if you call list.Append(1.1); you will get a not supported exception.

like image 35
Henk Holterman Avatar answered Oct 12 '22 02:10

Henk Holterman