<p>How can I do the Ruby method <a href="http://www.ruby-doc.org/core/classes/Array.html#M002241" rel="noreferrer">"Flatten" Ruby Method</a> in C#. This method flattens a jagged array into a single-dimensional array.</p>

<p>For example:</p>

<pre><code>s = [ 1, 2, 3 ]           #=&gt; [1, 2, 3]
t = [ 4, 5, 6, [7, 8] ]   #=&gt; [4, 5, 6, [7, 8]]
a = [ s, t, 9, 10 ]       #=&gt; [[1, 2, 3], [4, 5, 6, [7, 8]], 9, 10]
a.flatten                 #=&gt; [1, 2, 3, 4, 5, 6, 7, 8, 9, 10
</code></pre>

Recursive solution:
```
```
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;
        }
    }
}
```
```
EDIT 1:
Jon
explains in the comments why it cannot be a generic method, take a look!
EDIT 2:
Matt
suggested making it an extension method. Here you go, just replace the first line with:
```
```
public static IEnumerable Flatten(this IEnumerable array)
```
```
and you can use it like this:
```
```
foreach(var item in myArray.Flatten()) { ... }
```
```