Iterates through a sequence, calling a delegate for each element in it. - CSharp System.Collections.Generic

CSharp examples for System.Collections.Generic:IEnumerable

Description

Iterates through a sequence, calling a delegate for each element in it.

Demo Code


using System.Text;
using System.Linq;
using System.Collections.Generic;
using System;// w  w w  .  ja  va2 s . c  om

public class Main{
        /// <summary>
    ///   <para>Iterates through a sequence, calling a delegate for each element in it.</para>
    /// </summary>
    /// <typeparam name="T">Type of elements in a sequence.</typeparam>
    /// <param name="self">Source sequence for iteration.</param>
    /// <param name="action">Delegate to be called for each element in a sequence.</param>
    /// <returns>Back reference to the current sequence.</returns>
    /// <exception cref="ArgumentNullException">If either <paramref name="self"/> or <paramref name="action"/> is a <c>null</c> reference.</exception>
    public static IEnumerable<T> Each<T>(this IEnumerable<T> self, Action<T> action)
    {
      Assertion.NotNull(self);
      Assertion.NotNull(action);

      foreach (var value in self)
      {
        action(value);
      }

      return self;
    }
}

Related Tutorials