fork download
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Text;
  5.  
  6. class Program
  7. {
  8. static void Main(string[] args)
  9. {
  10. //Test with classes
  11. string[] classesList1 = new[] {"a", "b", "c"};
  12. string[] classesList2 = new[] { "1", "2", "3" };
  13. List<string[]> classesListCombined = new List<string[]> { classesList1, classesList2 };
  14.  
  15. Console.WriteLine("classesList1: {0}", classesList1.Join());
  16. Console.WriteLine("classesListCombined: {0}", classesList1.Join());
  17.  
  18. //Test with structs
  19. int[] structList1 = new[] { 1, 2, 3 };
  20. int[] structList2 = new[] { 4, 5, 6 };
  21. List<int[]> structListCombined = new List<int[]> { structList1, structList2 };
  22.  
  23.  
  24. Console.WriteLine("structList1: {0}", structList1.Join());
  25. Console.WriteLine("structListCombined: {0}", structListCombined.Join());
  26.  
  27. //Test tripple nesting
  28.  
  29. var nest1 = new[] {1, 2, 3};
  30. var nest2 = new[] { 4, 5, 6 };
  31. var nest3 = new[] { 7, 8, 9 };
  32. var nest4 = new[] {10, 11, 12};
  33.  
  34. var layer1 = new[] {nest1, nest2};
  35. var layer2 = new[] {nest3, nest4};
  36. var top = new[] {layer1, layer2};
  37.  
  38.  
  39. Console.WriteLine("top: {0}", top.Join());
  40.  
  41. Console.ReadLine();
  42.  
  43. }
  44. }
  45.  
  46. internal static class ExtensionMethods
  47. {
  48. public static String Join<T>(this IEnumerable<T> enumerable)
  49. {
  50. StringBuilder sb = new StringBuilder();
  51. JoinInternal(enumerable, sb, true);
  52. return sb.ToString();
  53. }
  54.  
  55. private static bool JoinInternal(IEnumerable enumerable, StringBuilder sb, bool first)
  56. {
  57. foreach (var item in enumerable)
  58. {
  59. var castItem = item as IEnumerable;
  60. if (castItem != null)
  61. {
  62. first = JoinInternal(castItem, sb, first);
  63. }
  64. else
  65. {
  66. if (!first)
  67. {
  68. sb.Append(",");
  69. }
  70. else
  71. {
  72. first = false;
  73. }
  74.  
  75. sb.Append(item);
  76. }
  77. }
  78. return first;
  79. }
  80. }
Success #stdin #stdout 0.05s 24344KB
stdin
Standard input is empty
stdout
classesList1: a,b,c
classesListCombined: a,b,c
structList1: 1,2,3
structListCombined: 1,2,3,4,5,6
top: 1,2,3,4,5,6,7,8,9,10,11,12