using System; using System.Collections.Generic; using System.Text; namespace System.Linq { public static partial class Enumerable { public static IEnumerable Concat (this IEnumerable first, IEnumerable second) { if (first == null) throw new ArgumentException ("first"); if (second == null) throw new ArgumentException ("second"); foreach (TSource element in first) yield return element; foreach (TSource element in second) yield return element; } public static IEnumerable Union (this IEnumerable first, IEnumerable second) { return first.Concat (second).Distinct (); } public static IEnumerable Intersect (this IEnumerable first, IEnumerable second) { if (first == null) throw new ArgumentException ("first"); if (second == null) throw new ArgumentException ("second"); var firstDict = new Dictionary(); foreach (TSource element in first) firstDict [element] = false; foreach (TSource element in second) if (firstDict.ContainsKey (element)) firstDict [element] = true; foreach (KeyValuePair keyValue in firstDict) if (keyValue.Value) yield return keyValue.Key; } public static IEnumerable Except (this IEnumerable first, IEnumerable second) { if (first == null) throw new ArgumentException ("first"); if (second == null) throw new ArgumentException ("second"); Dictionary firstDict = new Dictionary (); foreach (TSource element in first) firstDict [element] = null; foreach (TSource element in second) firstDict.Remove (element); foreach (TSource element in firstDict.Keys) yield return element; } } }