There is no AddRange()
method for IList
.
How can I add a list of items to an IList
without iterating through the
You could also write an extension method like this:
internal static class EnumerableHelpers
{
public static void AddRange(this IList collection, IEnumerable items)
{
foreach (var item in items)
{
collection.Add(item);
}
}
}
Usage:
IList collection = new MyCustomList(); //Or any other IList except for a fixed-size collection like an array
var items = new[] {1, 4, 5, 6, 7};
collection.AddRange(items);
Which is still iterating over items, but you don't have to write the iteration or cast every time you call it.