As a new .NET 3.5 programmer, I started to learn LINQ and I found something pretty basic that I haven't noticed before:
The book claims every array implements IEnumerable<T>
(obviously, otherwise we couldn't use LINQ to objects on arrays...). When I saw this, I thought to myself that I never really thought about that, and I asked myself what else all arrays implement - so I examined System.Array
using the object browser (since it's the base class for every array in the CLR) and, to my surprise, it doesn't implement IEnumerable<T>
.
So my question is: where is the definition? I mean, how can I tell exactly which interfaces every array implements?
The Array class is the base class for all the arrays in C#. It is defined in the System namespace. The Array class provides various properties and methods to work with arrays.
The C# language imposes no limit on the number of interfaces.
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).
In C# versions earlier than 8.0, an interface is like an abstract base class with only abstract members. A class or struct that implements the interface must implement all its members. Beginning with C# 8.0, an interface may define default implementations for some or all of its members.
From the documentation (emphasis mine):
[...] the Array class implements the
System.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.
EDIT: as Jb Evain points out in his comment, only vectors (one-dimensional arrays) implement the generic interfaces. As to why multi-dimensional arrays don't implement the generic interfaces, I'm not quite sure since they do implement the non-generic counterparts (see the class declaration below).
The System.Array
class (i.e. every array) also implements these non-generic interfaces:
public abstract class Array : ICloneable, IList, ICollection, IEnumerable, IStructuralComparable, IStructuralEquatable
You can find the answer to your question empirically using a small code snippet:
foreach (var type in (new int[0]).GetType().GetInterfaces()) Console.WriteLine(type);
Running the above snippet would result in the following output (on .NET 4.0
):
System.ICloneable System.Collections.IList System.Collections.ICollection System.Collections.IEnumerable System.Collections.IStructuralComparable System.Collections.IStructuralEquatable System.Collections.Generic.IList`1[System.Int32] System.Collections.Generic.ICollection`1[System.Int32] System.Collections.Generic.IEnumerable`1[System.Int32]
(`1
means <T>
)
After .NET 4.5
(.NET Standard 1.0
and later), there's two additional interfaces:
System.Collections.Generic.IReadOnlyList`1[System.Int32] System.Collections.Generic.IReadOnlyCollection`1[System.Int32]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With