using System;
using System.Text;
using System.Collections.Generic;
namespace ExtensionMethods
{
///
/// Add extensions to List
///
public static class ListExtensions
{
///
/// Adds an item to the if it does not yet exist
///
/// The type of the
///
/// The object to add to the
/// Thrown by
public static void AddUnique(this ICollection list, T item)
{
if (!list.Contains(item))
list.Add(item);
}
///
/// Concatenates a speceified separator between each element of a specified array, yielding a single concatenated string
///
///
/// A
/// The concatenated
public static string Join(this ICollection array, string separator)
{
StringBuilder concat = new StringBuilder();
bool notFirst = false;
foreach (T item in array)
{
if (notFirst)
concat.Append(separator);
else
notFirst = true;
concat.Append(item.ToString());
}
return concat.ToString();
}
///
/// Intersects to s
///
///
///
///
/// A that only contains the items which occur both in and
public static List Intersect(this ICollection list1, ICollection list2)
{
List inter = new List(list1.Count + list2.Count);
List merge = new List(list1);
merge.AddRange(list2);
foreach (T item in merge)
if (list1.Contains(item) && list2.Contains(item))
inter.Add(item);
return inter;
}
}
}