/// <summary> /// Interact throught an instance of <see cref="IEnumerable{T}"/>. /// And pass an index value to the interator action. /// </summary> /// <typeparam name="T">The enumerable argument type.</typeparam> /// <param name="source">An instance of <see cref="IEnumerable{T}"/>.</param> /// <param name="action">The interator action.</param> /// <exception cref="ArgumentNullException"> /// if <paramref name="source"/> or <paramref name="action"/> were <c>null</c>. /// </exception> public static void Each <T>(this IEnumerable <T> source, Action <T, int> action) { Prevent.ParameterNull(action, nameof(action)); if (source == null) { return; } var counter = 0; using (var enumerator = source.GetEnumerator()) { while (enumerator.MoveNext()) { action(enumerator.Current, counter++); } } }
/// <summary> /// Interact throught an instance of <see cref="IEnumerable"/>. /// And pass an index value to the interator action. /// </summary> /// <param name="source">An instance of <see cref="IEnumerable"/>.</param> /// <param name="action">The interator action.</param> /// <exception cref="ArgumentNullException"> /// if <paramref name="source"/> or <paramref name="action"/> were <c>null</c>. /// </exception> public static void Each(this IEnumerable source, Action <object, int> action) { Prevent.ParameterNull(action, nameof(action)); if (source == null) { return; } var counter = 0; var enumerator = source.GetEnumerator(); while (enumerator.MoveNext()) { action(enumerator.Current, counter++); } var disposable = enumerator as IDisposable; if (disposable != null) { disposable.Dispose(); } }
/// <summary> /// Interact throught an instance of <see cref="IEnumerable"/>. /// </summary> /// <param name="source">An instance of <see cref="IEnumerable"/>.</param> /// <param name="action">The interator action.</param> /// <exception cref="ArgumentNullException"> /// if <paramref name="source"/> or <paramref name="action"/> were <c>null</c>. /// </exception> public static void Each(this IEnumerable source, Action <object> action) { Prevent.ParameterNull(action, nameof(action)); Each(source, (current, _) => action(current)); }