2

Why would the Count property be an explicit interface implementation in System.Array?

    string[] a = new string[0];
    int countAsArray = a.Count; // compile error
    int countAsIList = ((ICollection<string>)a).Count; // success

http://msdn.microsoft.com/en-us/library/bb357392.aspx

Zaid Masud
  • 13,225
  • 9
  • 67
  • 88

5 Answers5

7

Probably because the more common means of determining the number of items in an Array is the Length property. Having both Count and Length would be a tad confusing.

vcsjones
  • 138,677
  • 31
  • 291
  • 286
3

If you have an array already, you don't need to be able to call array.Count, because there's already array.Length which does the same thing. If both Count and Length were directly available, we'd be getting pointless discussions of which to use.

2

Likely because Array already have Length property with the same meaning from 1.0 version.

Edit:

As pointed out 1.0 already had ICollection interface (version information, so 1.0 reference in my original guess is less likely to be valid. Another likely reason is that enough existing languages/libraries used Length property for length of array.

There are interesting comments on the subject at count vs length vs size in a collection.

Community
  • 1
  • 1
Alexei Levenkov
  • 98,904
  • 14
  • 127
  • 179
  • I am accepting this answer because it mentions the 1.0 version and I think the backwards compatibility is key to this design decision. If they were to do it again they probably would have only a .Count property on System.Array and no .Length property. In fact I think they should've considered naming the IList.Count property IList.Length instead. – Zaid Masud Mar 23 '12 at 17:36
  • 2
    @zooone9243 `ICollection` existed since .NET 1.0 too in it's [non-generic form](http://msdn.microsoft.com/en-us/library/system.collections.icollection(v=vs.71).aspx). I'm not entirely sure where backwards compatibility falls into this. – vcsjones Mar 23 '12 at 17:45
  • 1
    @vcsjones That's 1.1, not 1.0. I am unable to find references for 1.0, but I was wondering the same thing. –  Mar 23 '12 at 17:49
  • @hvd It's pretty much impossible to find any documentation around 1.0; however the [Version Information](http://msdn.microsoft.com/en-us/library/system.collections.icollection.aspx) for `ICollection` indicates it existed in .NET 1.0. I would be extremely surprised if this change occurred between 1.0 and 1.1. – vcsjones Mar 23 '12 at 17:51
  • @vcsjones you are right, on looking at this System.Array implemented ICollection in .NET 1.0 as well. I wonder why they didn't name it Array.Count then. – Zaid Masud Mar 23 '12 at 17:53
  • 1
    @zooone9243 because an array is something that has a length; a collection may not be. For example, a collection could be a set or a tree; these are not linear in nature. – phoog Mar 23 '12 at 18:09
  • Integrated comments into the answer... I like vcsjones's answer more :). – Alexei Levenkov Mar 23 '12 at 18:37
2

Arrays already have a .Length property. Having .Count as an explicit implementation hides duplicate functionality.

David
  • 1,143
  • 7
  • 9
0

Probably because there's already a Length property that does the same thing. Therefore, for performance reasons they'd want you to use Length instead of Count, which would do nothing but wrap Length anyway. The explicit implementation is there for being able to interoperate with List and other such things.

lobsterism
  • 3,469
  • 2
  • 22
  • 36
  • 1
    Generally I think you are on the right track, but this would be more a design-driven decision than a performance-driven one. – Zaid Masud Mar 23 '12 at 17:35