Skip to content

Instantly share code, notes, and snippets.

@bbqchickenrobot
Last active January 21, 2019 22:12
Show Gist options
  • Select an option

  • Save bbqchickenrobot/a758f51c76ca53612b4c312b18b690e0 to your computer and use it in GitHub Desktop.

Select an option

Save bbqchickenrobot/a758f51c76ca53612b4c312b18b690e0 to your computer and use it in GitHub Desktop.
Flatten Array - Citrus Byte
using System;
using System.Collections;
public static class Program{
public static void Main(string[] args = null){
var list = new object[]{1, 2, new int[]{3, 5}, 4, new object[]{34, 4, null, 23, 0}, null};
object[] flatten(object obj){
var flattend = new List<object>();
if(obj != null && obj.GetType().IsArray){
foreach(var o in obj as ICollection){
flattend.AddRange(flatten(o));
}
}else{
flattend.Add(obj ?? "null");
}
return flattend.ToArray<object>();
}
var flattened = flatten(list);
foreach(var f in flattened)
Console.WriteLine(f);
}
}
// Program.Main(); // use if running from other main method... or just run this in debugger
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment