Merging collections
JK | Thu, 2008/12/04 - 14:44
ICollection does not offer a method for merging two collections. As merging collections was exactly what I needed, I wrote a bit of code for it. An extension method seemed to offer the most elegant solution:
public static ICollection<T> Add<T>(this IList<T> self, IList<T> input)
{
foreach (T item in input)
{
self.Add(item);
}
return self;
}
Because I've overloaded the Add method, this will now work:
IList<string> list1 = new IList<string>(); IList<string> list2 = new IList<string>(); string name = 'J2'; list1.Add(list2); list1.Add(name);
For the license to this code, see http://creativecommons.org/licenses/by/3.0/
