Applies the action to every item in the list Deals with null IList - CSharp System.Collections.Generic

CSharp examples for System.Collections.Generic:IList

Description

Applies the action to every item in the list Deals with null IList

Demo Code


using System.Linq;
using System.Collections.Generic;
using System.Collections;
using System;//w  w w.jav a  2  s  . c  o  m
using Cirrious.CrossCore.Platform;

public class Main{
        /// <summary>
        /// Applies the action to every item in the list
        /// Deals with null IList
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="source">The source.</param>
        /// <param name="action">The action.</param>
        public static IList<T> SafeForEach<T>(this IList<T> source, Action<T> action)
        {
            if (source != null && source.Count > 0)
            {
                foreach (var item in source)
                {
                    action(item);
                }
            }

            return source;
        }
}

Related Tutorials