我刚才注意到一个多维数组在C#中,不执行的IEnumerable< T> ,而它确实实现的IEnumerable 。对于一维数组,无论是的IEnumerable< T> 和的IEnumerable 的实施。
I have just noticed that a multidimensional array in C#, does not implement IEnumerable<T>, while it does implement IEnumerable. For single-dimensional arrays, both IEnumerable<T> and IEnumerable are implemented.
为什么这种差异?如果一个多维数组是的IEnumerable ,当然它也应该实现通用版本?我注意到这一点,因为我想在一个多维数组,除非你使用演员LT从而未能使用扩展方法; T&GT; 或类似的;所以我可以肯定看到讨论是否多维数组实施的IEnumerable&LT; T&GT;
Why this difference? If a multi-dimensional array is IEnumerable, surely it should also implement the generic version? I noticed this because I tried to use an Extension method on a multidimensional array, which fails unless you use Cast<T> or similar; so I can definitely see the an argument for making multidimensional arrays implement IEnumerable<T>.
要澄清我在code的问题,我希望下面的code打印真的四倍,而它实际打印真,假,真,真:
To clarify my question in code, I would expect the following code to print true four times, while it actually prints true, false, true, true:
int[] singleDimensionArray = new int[10]; int[,] multiDimensional = new int[10, 10]; Debug.WriteLine(singleDimensionArray is IEnumerable<int>); Debug.WriteLine(multiDimensional is IEnumerable<int>); Debug.WriteLine(singleDimensionArray is IEnumerable); Debug.WriteLine(multiDimensional is IEnumerable);推荐答案
的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&LT; U&GT; (§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.
更多推荐
为什么C#多维数组没有实现IEnumerable&LT; T&GT;?
发布评论