How can I do the Ruby method ‘Flatten’ Ruby Method in C#. This method flattens a jagged array into a single-dimensional array.
For example:
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
Recursive solution:
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:
and you can use it like this: