According to MSDN docs, System.Array implements ICollection, yet System.Array does not provide a Count property (of course you can always use the LINQ Count() extension method, but there is no property with this name). How can this be? Isn't Count required?
- 17,541
- 8
- 92
- 91
- 12,851
- 5
- 46
- 75
-
1Yes, here's your `Count` property: http://msdn.microsoft.com/en-us/library/bb357392.aspx Why it's explicitly implemented instead of implicitly is a question for the framework designers – Matt Burland May 01 '14 at 20:39
-
2`((ICollection)theArray).Count` – Erik Funkenbusch May 01 '14 at 20:42
-
1Yes, for example, you can call `Add` (defined in `ICollection`) on the array and get an exception. – Matthew May 01 '14 at 20:46
-
3@MattBurland explicitly implemented due to DRY I imagine. `Array` already has `Length` and the explicit `Count` just returns `Length` for that interface. Having `Length` and `Count` on the same class doing the same thing would be silly. – Haney May 01 '14 at 20:47
-
1@Matthew: You are confusing `System.Collection.ICollection` with `System.Collection.Generic.ICollection`. The `ICollection` interface doesn't have an `Add` method. `ICollection
` does, but that wasn't the question. – Matt Burland May 01 '14 at 20:49 -
4@DavidHaney: Probably true. Although it's not so much DRY as hiding the fact that they are repeating themselves. – Matt Burland May 01 '14 at 20:49
-
1@MattBurland you're right, my mistake. – Matthew May 01 '14 at 20:50
3 Answers
It's explicitly implemented like so:
int ICollection.Count
{
get
{
return Length;
}
}
You can read more about explicit interface implementation on MSDN.
- 138,677
- 31
- 291
- 286
From Array.ICollection.Count Property - MSDN
This member is an explicit interface member implementation. It can be used only when the
Arrayinstance is cast to aSystem.Collections.ICollectioninterface.
- 17,541
- 8
- 92
- 91
- 219,104
- 29
- 407
- 436
From the documentation:
Starting with the .NET Framework 2.0, the
Arrayclass implements theSystem.Collections.Generic.IList<T>,System.Collections.Generic.ICollection<T>, andSystem.Collections.Generic.IEnumerable<T>generic interfaces. The implementations are provided to arrays at run time, and therefore are not visible to the documentation build tools. As a result, the generic interfaces do not appear in the declaration syntax for theArrayclass, and there are no reference topics for interface members that are accessible only by casting an array to the generic interface type (explicit interface implementations). The key thing to be aware of when you cast an array to one of these interfaces is that members which add, insert, or remove elements throwNotSupportedException.
- 17,541
- 8
- 92
- 91
- 2,040
- 13
- 17
-
But the question is about [System.Collections.ICollections](http://msdn.microsoft.com/en-us/library/system.collections.icollection.aspx) not the generic [System.Collections.Generic.ICollection](http://msdn.microsoft.com/en-us/library/92t2ye13.aspx) – Matt Burland May 01 '14 at 20:43
-