Add range to Collection : ICollection « Collections Data Structure « C# / C Sharp






Add range to Collection

      

using System;
using System.Collections.Generic;

namespace Zero.Common
{
    /// <summary>
    /// Extension methods for cleaner code.
    /// </summary>
    public static class ExtensionMethods
    {
        /// <summary>
        /// Adds range of items into collection.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="collection"></param>
        /// <param name="items"></param>
        public static void AddRange<T>(this ICollection<T> collection, IEnumerable<T> items)
        {
            if (items == null)
            {
                System.Diagnostics.Debug.WriteLine("Do extension metody AddRange byly poslany items == null");
                return;
            }
            foreach (var item in items)
            {
                collection.Add(item);
            }
        }

        /// <summary>
        /// Clears collection and adds range of items into it.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="collection"></param>
        /// <param name="items"></param>
        public static void ClearAndAddRange<T>(this ICollection<T> collection, IEnumerable<T> items)
        {
            collection.Clear();
            collection.AddRange(items);
        }

        /// <summary>
        /// Strong-typed object cloning for objects that implement <see cref="ICloneable"/>
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static T Clone<T>(this T obj) where T : ICloneable
        {
            return (T)(obj as ICloneable).Clone();
        }
    }
}

   
    
    
    
    
    
  








Related examples in the same category

1.Finds a value of the given type in the given collection.
2.Adds a new element to the specified collection.
3.Adds all of the elements of the "c" collection to the "target" collection.
4.Removes all the elements from the collection.
5.Determines whether the collection contains the specified element.
6.Removes the specified element from the collection.
7.Retains the elements in the target collection that are contained in the specified collection
8.Returns an array containing all the elements of the collection.
9.Converts an ICollection instance to an ArrayList instance.
10.Tests if the specified object is a collection and converts it to its string representation.
11.Determines whether the collection contains the specified element
12.Adds the specified element to the specified collection
13.Determines whether the collection contains all the elements in the specified collection.
14.Removes all the elements from the target collection that are contained in the source collection.
15.Converts an System.Collections.ICollection instance to an System.Collections.ArrayList instance.
16.Copies the elements of the ICollection to a new array of the specified element type.
17.Determine whether a given collection only contains a single unique object
18.Is a Collection Null Or Empty Or Default
19.Converts the specified collection to its string representation.
20.Group the collection using a function which returns the key.
21.Convert ICollection to T[]
22.Convert ICollection to T[]
23.ConvertAll ICollection to TOut[] with Converter
24.Lambda Collections Generic Set