Why do C# multidimensional arrays not implement IEnumerable?

The CLR has two different kinds of arrays: vectors which are guaranteed to be one-dimensional with a lower bound of 0, and more general arrays which can have non-zero bounds and a rank other than 0.

From section 8.9.1 of the CLI spec:

Additionally, a created vector with
element type T, implements the
interface
System.Collections.Generic.IList<U>
(§8.7), where U := T.

I have to say it seems pretty weird to me. Given that it already implements IEnumerable I don’t see why it shouldn’t implement IEnumerable<T>. It wouldn’t make as much sense to implement IList<T>, but the simple generic interface would be fine.

If you want this, you could either call Cast<T> (if you’re using .NET 3.5) or write your own method to iterate through the array. To avoid casting you’d have to write your own method which found the lower/upper bounds of each dimension, and fetched things that way. Not terribly pleasant.

Leave a Comment