There is no AddRange()
method for IList
.
How can I add a list of items to an IList
without iterating through the
If you look at the C# source code for List
Poking around the source code, you see the .NET folks do similar things in their own LINQ extensions for things like .ToList() (if it is a list, cast it... otherwise create it).
public static class IListExtension
{
public static void AddRange(this IList list, IEnumerable items)
{
if (list == null) throw new ArgumentNullException(nameof(list));
if (items == null) throw new ArgumentNullException(nameof(items));
if (list is List asList)
{
asList.AddRange(items);
}
else
{
foreach (var item in items)
{
list.Add(item);
}
}
}
}