在C#中展平Ruby方法

如何在C#中使用Ruby方法“展平”Ruby方法 。 该方法将锯齿状arrays展平为单维arrays。

例如:

s = [ 1, 2, 3 ] #=> [1, 2, 3] t = [ 4, 5, 6, [7, 8] ] #=> [4, 5, 6, [7, 8]] a = [ s, t, 9, 10 ] #=> [[1, 2, 3], [4, 5, 6, [7, 8]], 9, 10] a.flatten #=> [1, 2, 3, 4, 5, 6, 7, 8, 9, 10 

递归解决方案:

 IEnumerable Flatten(IEnumerable array) { foreach(var item in array) { if(item is IEnumerable) { foreach(var subitem in Flatten((IEnumerable)item)) { yield return subitem; } } else { yield return item; } } } 

编辑1:

Jon在评论中解释为什么它不能成为通用方法,看一看!

编辑2:

马特建议将其作为一种扩展方法。 在这里,您只需将第一行替换为:

 public static IEnumerable Flatten(this IEnumerable array) 

你可以像这样使用它:

 foreach(var item in myArray.Flatten()) { ... } 

我会在评论中回复,但我需要超过300个字符。

@ Alexander的解决方案非常棒,但它遇到了字符串数组的问题。 由于string实现了IEnumerable,我认为它最终将返回每个字符串中的每个字符。 您可以使用通用参数来告诉它您希望在这些情况下返回的内容,例如:

 public static IEnumerable Flatten(IEnumerable e) { if (e == null) yield break; foreach (var item in e) { if (item is T) yield return (T)item; else if (item is IEnumerable) { foreach (var subitem in Flatten((IEnumerable)item)) yield return subitem; } else yield return item; } } 

难道你不能只使用IEnumerable#SelectMany吗?