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
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
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.
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.
Arrays already have a .Length
property. Having .Count
as an explicit implementation hides duplicate functionality.
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.