Last active
January 21, 2019 22:12
-
-
Save bbqchickenrobot/a758f51c76ca53612b4c312b18b690e0 to your computer and use it in GitHub Desktop.
Flatten Array - Citrus Byte
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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