Further thoughts on the ObservableCollection extensions

Firstly (thanks Matt) – it should really be an ICollection extension – and so now it is, but also I’ve just ported it to a .NET 4.0 project, and can now make use of optional parameters, and so 3 methods now become 1!


/// <summary>Adds a range of items to an <see cref="ICollection{T}"/>.</summary>
/// <typeparam name="T">The type held in the <see cref="ICollection{T}"/>.</typeparam>
/// <param name="collection">The <see cref="ICollection{T}"/> instance to add the items to.</param>
/// <param name="items">The items to add the <see cref="ICollection{T}"/>.</param>
/// <param name="filter">[Optional] A filter to use to select specific values from the <paramref name="items"/>.</param>
/// <param name="transform">[Optional] A transform to apply to each of the <paramref name="items"/>.</param>
/// <exception cref="ArgumentNullException">Thrown if the <paramref name="collection"/> given is null.</exception>
/// <exception cref="ArgumentNullException">Thrown if the <paramref name="items"/> given is null.</exception>
public static void AddRange<T>(this ICollection<T> collection, IEnumerable<T> items, Func<T, bool> filter = null, Func<T, T> transform = null)
    Throw.IfNull("collection", collection);
    Throw.IfNull("items", items);
    if (filter == null)
        filter = t => true;
    if (transform == null)
        transform = t => t;
    foreach (T item in items.Where(filter))


Print | posted @ Tuesday, August 10, 2010 8:29 AM

Comments on this entry:

No comments posted yet.

Post A Comment