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.
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>
.
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);
Question&Answers:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…