Adds each item in IEnumerable to ICollection. - CSharp System.Collections.Generic

CSharp examples for System.Collections.Generic:IEnumerable

Description

Adds each item in IEnumerable to ICollection.

Demo Code


using System.Collections.ObjectModel;
using System.Collections.Generic;
using System;//from   w w  w  . ja v a2  s .  com

public class Main{
        /// <summary>
        /// Adds each item in <paramref name="additions"/> to <paramref name="collection"/>.
        /// </summary>
        /// <typeparam name="T">The type of the items in the collection.</typeparam>
        /// <param name="collection">The collection to which items should be added.</param>
        /// <param name="additions">The collection of items to be added.</param>
        /// <remarks>
        /// This is an <i>O(n)</i> operation where <i>n</i> is <code>additions.Count</code>.
        /// </remarks>
        public static void AddAll<T>(this ICollection<T> collection, IEnumerable<T> additions)
        {
            if (collection == null)
                throw new ArgumentNullException("collection");

            if (additions == null)
                throw new ArgumentNullException("additions");

            if (collection.IsReadOnly)
                throw new InvalidOperationException("collection is read only");

            foreach (var item in additions)
                collection.Add(item);
        }
}

Related Tutorials