且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

为什么C#多维数组没有实现IEnumerable< T>?

更新时间:2022-11-27 21:56:02

的CLR有阵列的两种不同类型:载体的这保证是一维具有下界0,和其可具有非零边界和秩0以外更一般的阵列

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.

在CLI规范第8.9.1:

From section 8.9.1 of the CLI spec:

此外,一个创建的载体   元素类型T,实现了   接口    System.Collections.Generic.IList< U>   (§8.7),其中U:= T

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

我不得不说,这似乎pretty的怪我。由于它已经实现了的IEnumerable 我不明白为什么它不应该执行的IEnumerable&LT; T&GT; 。它不会作出太大的意义落实的IList&LT; T&GT; ,但简单的通用接口就可以了。

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.

如果你想这样,你既可以调用演员LT; T&GT; (如果你使用.NET 3.5)或写你自己的方法来遍历数组。为了避免铸造你必须编写其中发现每个维度的下限/上限,并取东西,这样你自己的方法。并不十分愉快的。

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.