Adds the range of items. Deals with null collections - CSharp System.Collections.Generic

CSharp examples for System.Collections.Generic:ICollection

Description

Adds the range of items. Deals with null collections

Demo Code


using System.Linq;
using System.Collections.Generic;
using System;/*from   w  ww. j av a 2 s. c om*/

public class Main{
        /// <summary>
        /// Adds the range of items.
        /// Deals with null collections
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="source">The source.</param>
        /// <param name="items">The items.</param>
        public static void SafeAddRange<T>(this ICollection<T> source, IEnumerable<T> items)
        {
            if (source == null || items == null)
                return;

            try
            {
                if (items != null && items.Count() > 0)
                {
                    foreach (T item in items)
                    {
                        source.Add(item);
                    }
                }
            }
            catch (Exception)
            {
                //TODO: Log this error
            }
        }
}

Related Tutorials