code_junkie

Console.WriteLine and generic List

Nov 10th, 2011
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.23 KB | None | 0 0
  1. List<int> list = new List<int> { 1, 3, 5 };
  2. foreach (int i in list) {
  3. Console.Write("{0}t", i.ToString()); }
  4. Console.WriteLine();
  5.  
  6. List<int> list = new List<int> { 1, 3, 5 };
  7. Console.WriteLine("{0}t", list);
  8.  
  9. list.ForEach(i => Console.Write("{0}t", i));
  10.  
  11. Console.WriteLine(string.Join("t", list.Cast<string>().ToArray()));
  12.  
  13. list.ForEach(i => Console.Write("{0}t", i));
  14.  
  15. public static void WriteLine(this List<int> theList)
  16. {
  17. foreach (int i in list)
  18. {
  19. Console.Write("{0}t", t.ToString());
  20. }
  21. Console.WriteLine();
  22. }
  23.  
  24. public static void WriteToConsole<T>(this IList<T> collection)
  25. {
  26. int count = collection.Count();
  27. for(int i = 0; i < count; ++i)
  28. {
  29. Console.Write("{0}t", collection[i].ToString(), delimiter);
  30. }
  31. Console.WriteLine();
  32. }
  33.  
  34. public static void WriteToConsole<T>(this IList<T> collection)
  35. {
  36. WriteToConsole<T>(collection, "t");
  37. }
  38.  
  39. public static void WriteToConsole<T>(this IList<T> collection, string delimiter)
  40. {
  41. int count = collection.Count();
  42. for(int i = 0; i < count; ++i)
  43. {
  44. Console.Write("{0}{1}", collection[i].ToString(), delimiter);
  45. }
  46. Console.WriteLine();
  47. }
  48.  
  49. using System;
  50. using System.Collections.Generic;
  51. using System.Linq;
  52.  
  53. namespace ConsoleWritelineTest
  54. {
  55. public static class Extensions
  56. {
  57. public static void WriteToConsole<T>(this IList<T> collection)
  58. {
  59. WriteToConsole<T>(collection, "t");
  60. }
  61.  
  62. public static void WriteToConsole<T>(this IList<T> collection, string delimiter)
  63. {
  64. int count = collection.Count();
  65. for(int i = 0; i < count; ++i)
  66. {
  67. Console.Write("{0}{1}", collection[i].ToString(), delimiter);
  68. }
  69. Console.WriteLine();
  70. }
  71. }
  72.  
  73. internal class Foo
  74. {
  75. override public string ToString()
  76. {
  77. return "FooClass";
  78. }
  79. }
  80.  
  81. internal class Program
  82. {
  83.  
  84. static void Main(string[] args)
  85. {
  86. var myIntList = new List<int> {1, 2, 3, 4, 5};
  87. var myDoubleList = new List<double> {1.1, 2.2, 3.3, 4.4};
  88. var myDoubleArray = new Double[] {12.3, 12.4, 12.5, 12.6};
  89. var myFooList = new List<Foo> {new Foo(), new Foo(), new Foo()};
  90. // Using the standard delimiter /t
  91. myIntList.WriteToConsole();
  92. myDoubleList.WriteToConsole();
  93. myDoubleArray.WriteToConsole();
  94. myFooList.WriteToConsole();
  95. // Using our own delimiter ~
  96. myIntList.WriteToConsole("~");
  97. Console.Read();
  98. }
  99. }
  100. }
  101.  
  102. list.ForEach(i => Console.Write("{0}t", i));
  103.  
  104. public static void FastForEach<T>(this IList<T> collection, Action<T> actionToPerform)
  105. {
  106. int count = collection.Count();
  107. for (int i = 0; i < count; ++i)
  108. {
  109. actionToPerform(collection[i]);
  110. }
  111. Console.WriteLine();
  112. }
  113.  
  114. public static void WriteToConsole<T>(this IList<T> collection, string delimiter)
  115. {
  116. collection.FastForEach(item => Console.Write("{0}{1}", item.ToString(), delimiter));
  117. }
  118.  
  119. using System;
  120. using System.Collections.Generic;
  121. using System.Linq;
  122.  
  123. namespace ConsoleWritelineTest
  124. {
  125. public static class Extensions
  126. {
  127. public static void WriteToConsole<T>(this IList<T> collection)
  128. {
  129. WriteToConsole<T>(collection, "t");
  130. }
  131.  
  132. public static void WriteToConsole<T>(this IList<T> collection, string delimiter)
  133. {
  134. collection.FastForEach(item => Console.Write("{0}{1}", item.ToString(), delimiter));
  135. }
  136.  
  137. public static void FastForEach<T>(this IList<T> collection, Action<T> actionToPerform)
  138. {
  139. int count = collection.Count();
  140. for (int i = 0; i < count; ++i)
  141. {
  142. actionToPerform(collection[i]);
  143. }
  144. Console.WriteLine();
  145. }
  146. }
  147.  
  148. internal class Foo
  149. {
  150. override public string ToString()
  151. {
  152. return "FooClass";
  153. }
  154. }
  155.  
  156. internal class Program
  157. {
  158.  
  159. static void Main(string[] args)
  160. {
  161. var myIntList = new List<int> {1, 2, 3, 4, 5};
  162. var myDoubleList = new List<double> {1.1, 2.2, 3.3, 4.4};
  163. var myDoubleArray = new Double[] {12.3, 12.4, 12.5, 12.6};
  164. var myFooList = new List<Foo> {new Foo(), new Foo(), new Foo()};
  165.  
  166. // Using the standard delimiter /t
  167. myIntList.WriteToConsole();
  168. myDoubleList.WriteToConsole();
  169. myDoubleArray.WriteToConsole();
  170. myFooList.WriteToConsole();
  171.  
  172. // Using our own delimiter ~
  173. myIntList.WriteToConsole("~");
  174.  
  175. // What if we want to write them to separate lines?
  176. myIntList.FastForEach(item => Console.WriteLine(item.ToString()));
  177. Console.Read();
  178. }
  179. }
  180. }
  181.  
  182. List<int> a = new List<int>() { 1, 2, 3, 4, 5 };
  183. a.ForEach(p => Console.WriteLine(p));
  184.  
  185. list.ForEach(x=>Console.WriteLine(x));
  186.  
  187. List<int> list = new List<int> { 1, 3, 5 };
  188. list.ForEach(x => Console.WriteLine(x));
  189.  
  190. public static void WriteLine(this List<int> theList)
  191. {
  192. foreach (int i in list)
  193. {
  194. Console.Write("{0}t", t.ToString());
  195. }
  196. Console.WriteLine();
  197. }
  198.  
  199. list.WriteLine();
Add Comment
Please, Sign In to add comment