traversing a collection with the action. - CSharp System.Collections.Generic

CSharp examples for System.Collections.Generic:ICollection

Description

traversing a collection with the action.

Demo Code


using System.Threading.Tasks;
using System.Text;
using System.Linq;
using System.Diagnostics.Contracts;
using System.Collections.Generic;
using System;//w  w  w  . j  a v  a  2 s .  c  o  m

public class Main{
        /// <summary>
        /// traversing a collection with the action.
        /// </summary>
        /// <typeparam name="TElement">element type of collection</typeparam>
        /// <param name="collection">the collection</param>
        /// <param name="action">the action to invoke</param>
        public static void Foreach<TElement>(this IEnumerable<TElement> collection,
            Action<TElement> action)
        {
            if (collection == null || action == null)
                throw new ArgumentNullException(collection == null ? "collection" : "action");

            foreach (TElement item in collection)
            {
                action.Invoke(item);
            }
        }
}

Related Tutorials