| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960 |
- // Licensed to the .NET Foundation under one or more agreements.
- // The .NET Foundation licenses this file to you under the Apache 2.0 License.
- // See the LICENSE file in the project root for more information.
- using System.Collections.Generic;
- namespace System.Linq
- {
- public static partial class EnumerableEx
- {
- /// <summary>
- /// Enumerates the sequence and invokes the given action for each value in the sequence.
- /// </summary>
- /// <typeparam name="TSource">Source sequence element type.</typeparam>
- /// <param name="source">Source sequence.</param>
- /// <param name="onNext">Action to invoke for each element.</param>
- public static void ForEach<TSource>(this IEnumerable<TSource> source, Action<TSource> onNext)
- {
- if (source == null)
- {
- throw new ArgumentNullException(nameof(source));
- }
- if (onNext == null)
- {
- throw new ArgumentNullException(nameof(onNext));
- }
- foreach (var item in source)
- {
- onNext(item);
- }
- }
- /// <summary>
- /// Enumerates the sequence and invokes the given action for each value in the sequence.
- /// </summary>
- /// <typeparam name="TSource">Source sequence element type.</typeparam>
- /// <param name="source">Source sequence.</param>
- /// <param name="onNext">Action to invoke for each element.</param>
- public static void ForEach<TSource>(this IEnumerable<TSource> source, Action<TSource, int> onNext)
- {
- if (source == null)
- {
- throw new ArgumentNullException(nameof(source));
- }
- if (onNext == null)
- {
- throw new ArgumentNullException(nameof(onNext));
- }
- var i = 0;
- foreach (var item in source)
- {
- onNext(item, checked(i++));
- }
- }
- }
- }
|