Question

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?

Was it helpful?

Solution

It's explicitly implemented like so:

int ICollection.Count
{
    get
    {
        return Length;
    }
}

You can read more about explicit interface implementation on MSDN.

OTHER TIPS

From Array.ICollection.Count Property - MSDN

This member is an explicit interface member implementation. It can be used only when the Array instance is cast to a System.Collections.ICollection interface.

From the documentation:

Starting with the .NET Framework 2.0, the Array class implements the System.Collections.Generic.IList<T>, System.Collections.Generic.ICollection<T>, and System.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 the Array class, 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 throw NotSupportedException.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top