Add Range Safely to ICollection - CSharp System.Collections

CSharp examples for System.Collections:ICollection

Description

Add Range Safely to ICollection

Demo Code


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

public class Main{
        public static void AddRangeSafely<T>(this ICollection<T> col, IEnumerable<T> items)
        {
            if(items == null) return;

            var list = col as List<T>;
            if (list != null)
            {
                list.AddRange(items);
            }
            else
            {
                foreach (var item in items)
                {
                    col.Add(item);
                }
            }
        }
}

Related Tutorials