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

Home
C# / C Sharp
1.2D Graphics
2.Class Interface
3.Collections Data Structure
4.Components
5.Data Types
6.Database ADO.net
7.Date Time
8.Design Patterns
9.Development Class
10.Event
11.File Stream
12.Generics
13.GUI Windows Form
14.Internationalization I18N
15.Language Basics
16.LINQ
17.Network
18.Office
19.Reflection
20.Regular Expressions
21.Security
22.Services Event
23.Thread
24.Web Services
25.Windows
26.Windows Presentation Foundation
27.XML
28.XML LINQ
C# / C Sharp » Collections Data Structure » ICollectionScreenshots 
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 objwhere 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
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.