diff --git a/LINQExtensions/LINQExtensions.cs b/LINQExtensions/LINQExtensions.cs index 8092f2f..7ac7937 100644 --- a/LINQExtensions/LINQExtensions.cs +++ b/LINQExtensions/LINQExtensions.cs @@ -113,6 +113,20 @@ public static void Shuffle(this List list) } } + /// + /// Applies the action to each element of the container in place. + /// + /// The type of elemtens in the container. + /// A container whose elements are to be affected. + /// The function to be applied to the elements in the container. + public static void ForEach(this IEnumerable container, Action action) + { + foreach (T element in container) + { + action(element); + } + } + /// /// Returns a random element from the array. /// diff --git a/LINQExtensions/README.md b/LINQExtensions/README.md index c94abb9..bcffc8b 100644 --- a/LINQExtensions/README.md +++ b/LINQExtensions/README.md @@ -4,7 +4,7 @@ A collection of extension methods for `IEnumerable`, `List` and arrays. ## Examples -# RandomElement/Shuffle/ToOneLineString +# RandomElement/Shuffle/ToOneLineString/ForEach ```C# int[] items = new int[] {1, 2, 3, 4, 5}; @@ -19,6 +19,9 @@ items.Shuffle(); // Example output: // "3", "5", "2", "1", "4" Debug.Log(items.ToOneLineString()); + +// Applies the function that takes the respective type to all the elements +items.ForEach(i => Debug.Log(i)); ``` # Nearest @@ -46,4 +49,4 @@ public class LINQExtensionsExamples : MonoBehaviour ## Dependencies -None. \ No newline at end of file +None.