Union two collections - CSharp System.Collections.Generic

CSharp examples for System.Collections.Generic:ICollection

Description

Union two collections

Demo Code

// This program is free software; you can redistribute it and/or modify it
using System.Collections.Generic;
using System.Collections;

public class Main{
        /// <summary>
        /// Union two collections
        /// </summary>
        /// <typeparam name="T">collection generic type parameter</typeparam>
        /// <param name="target">Target collection</param>
        /// <param name="source">Source collection</param>
        public static void UnionCollections<T>(ICollection<T> target, IEnumerable<T> source)
        {/*  w  ww .  j  a va  2s . com*/
            foreach (T item in source)
            {
                if (!target.Contains(item))
                    target.Add(item);
            }
        }
}

Related Tutorials