Loop Extensions

For Each

This performs an action on each element of a collection: list, array, dictionary, or enumerable.

List<int> list = new List<int> { 1, 2, 3 };

list .ForEach(num => Debug.Log(num));
int[] array = new int[] { 1, 2, 3 };

array.ForEach(num => Debug.Log(num));
Dictionary<string, int> dictionary = new Dictionary<string, int> { { "Alice", 100 }, { "Bob", 80 } };

dictionary.ForEach((key, value) => Debug.Log($"{key}: {value}"));
IEnumerable<int> enumerable = new List<int> { 1, 2, 3 };

enumerable.ForEach(num => Debug.Log(num));

Last updated