Guest User

Untitled

a guest
Apr 19th, 2018
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.07 KB | None | 0 0
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5.  
  6. namespace Foundation.Collections
  7. {
  8. /// <summary>
  9. /// Adds extension methods to the IEnumerable{T} interface.
  10. /// </summary>
  11. public static class EnumerableExtensions
  12. {
  13. /// <summary>
  14. /// Performs an action on the specified collection.
  15. /// </summary>
  16. /// <param name="collection">The collection.</param>
  17. /// <param name="action">The action.</param>
  18. /// <remarks>
  19. /// <para>
  20. /// Use the ForEach method to perform an action on a collection
  21. /// of items, one at a time, as you would when using a foreach loop.
  22. /// </para>
  23. /// <para>
  24. /// <example>
  25. /// <code>
  26. /// IEnumerable collection = new string[] {"a", "b", "c"};
  27. /// collection.ForEach(Console.WriteLine);
  28. /// </code>
  29. /// produces the following output:
  30. /// <code>
  31. /// a
  32. /// b
  33. /// c
  34. /// </code>
  35. /// </example>
  36. /// </para>
  37. /// </remarks>
  38. public static void ForEach(this IEnumerable collection, Action<object> action)
  39. {
  40. foreach (var item in collection)
  41. {
  42. action(item);
  43. }
  44. }
  45.  
  46. /// <summary>
  47. /// Performs an action on the specified collection.
  48. /// </summary>
  49. /// <param name="collection">The collection.</param>
  50. /// <param name="action">The action.</param>
  51. /// <remarks>
  52. /// <para>
  53. /// Use the ForEachWithIndex method to perform an action on a collection
  54. /// of items, one at a time, as you would when using a foreach loop.
  55. /// This method allows the action to consider an index, as well.
  56. /// </para>
  57. /// <para>
  58. /// <example>
  59. /// <code>
  60. /// IEnumerable collection = new string[] {"a", "b", "c"};
  61. /// collection.ForEachWithIndex(Console.WriteLine);
  62. /// </code>
  63. /// produces the following output:
  64. /// <code>
  65. /// a
  66. /// b
  67. /// c
  68. /// </code>
  69. /// </example>
  70. /// </para>
  71. /// </remarks>
  72. public static void ForEachWithIndex(this IEnumerable collection, Action<object, int> action)
  73. {
  74. int i = 0;
  75.  
  76. foreach (var item in collection)
  77. {
  78. action(item, i++);
  79. }
  80. }
  81.  
  82. /// <summary>
  83. /// Performs an action on the specified collection.
  84. /// </summary>
  85. /// <typeparam name="T">The type contained in the generic collection.</typeparam>
  86. /// <param name="collection">The collection.</param>
  87. /// <param name="action">The action.</param>
  88. /// <remarks>
  89. /// <para>
  90. /// Use the ForEach{T} method to perform an action on a collection
  91. /// of items, one at a time, as you would when using a foreach loop.
  92. /// </para>
  93. /// <para>
  94. /// <example>
  95. /// <code>
  96. /// IEnumerable{string} collection = new string[] {"a", "b", "c"};
  97. /// collection.ForEach(Console.WriteLine);
  98. /// </code>
  99. /// produces the following output:
  100. /// <code>
  101. /// a
  102. /// b
  103. /// c
  104. /// </code>
  105. /// </example>
  106. /// </para>
  107. /// </remarks>
  108. public static void ForEach<T>(this IEnumerable<T> collection, Action<T> action)
  109. {
  110. T[] items = collection.ToArray();
  111. int count = items.Count();
  112.  
  113. for (int i = 0; i < count; i++)
  114. {
  115. if (items[i] == null)
  116. {
  117. throw new NullReferenceException("The value of the item cannot be null.");
  118. }
  119.  
  120. action(items[i]);
  121. }
  122. }
  123.  
  124. /// <summary>
  125. /// Performs an action on the specified collection.
  126. /// </summary>
  127. /// <typeparam name="T">The type contained in the generic collection.</typeparam>
  128. /// <param name="collection">The collection.</param>
  129. /// <param name="action">The action.</param>
  130. /// <remarks>
  131. /// <para>
  132. /// Use the ForEachWithIndex{T} method to perform an action on a collection
  133. /// of items, one at a time, as you would when using a foreach loop.
  134. /// This method allows the action to consider an index, as well.
  135. /// </para>
  136. /// <para>
  137. /// <example>
  138. /// <code>
  139. /// IEnumerable{string} collection = new string[] {"a", "b", "c"};
  140. /// collection.ForEachWithIndex(Console.WriteLine);
  141. /// </code>
  142. /// produces the following output:
  143. /// <code>
  144. /// a
  145. /// b
  146. /// c
  147. /// </code>
  148. /// </example>
  149. /// </para>
  150. /// </remarks>
  151. public static void ForEachWithIndex<T>(this IEnumerable<T> collection, Action<T, int> action)
  152. {
  153. T[] items = collection.ToArray();
  154. int count = items.Count();
  155.  
  156. for (int i = 0; i < count; i++)
  157. {
  158. action(items[i], i);
  159. }
  160. }
  161. }
  162. }
Add Comment
Please, Sign In to add comment