It's explicitly implemented like so:
int ICollection.Count
{
get
{
return Length;
}
}
You can read more about explicit interface implementation on MSDN.
سؤال
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?
المحلول
It's explicitly implemented like so:
int ICollection.Count
{
get
{
return Length;
}
}
You can read more about explicit interface implementation on MSDN.
نصائح أخرى
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 aSystem.Collections.ICollection
interface.
From the documentation:
Starting with the .NET Framework 2.0, the
Array
class 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 theArray
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 throwNotSupportedException
.