How to add a range of items to an IList?
If you look at the C# source code for List<T>, I think List<T>.AddRange() has optimizations that a simple loop doesn't address. So, an extension method should simply check to see if the IList<T> is a List<T>, and if so use its native AddRange().
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<T>(this IList<T> list, IEnumerable<T> items)
{
if (list == null) throw new ArgumentNullException(nameof(list));
if (items == null) throw new ArgumentNullException(nameof(items));
if (list is List<T> asList)
{
asList.AddRange(items);
}
else
{
foreach (var item in items)
{
list.Add(item);
}
}
}
}
AddRange
is defined on List<T>
, not the interface.
You can declare the variable as List<T>
instead of IList<T>
or cast it to List<T>
in order to gain access to AddRange
.
((List<myType>)myIList).AddRange(anotherList);
This is not good practice (see comments below), as an IList<T>
might not be a List<T>
, but some other type that implemented the interface and may very well not have an AddRange
method - in such a case, you will only find out when your code throws an exception at runtime.
So, unless you know for certain that the type is indeed a List<T>
, you shouldn't try to use AddRange
.
One way to do so is by testing the type with the is or as operators (since C# 7).
if(myIList is List<T>)
{
// can cast and AddRange
}
else
{
// iterate with Add
}
You could do something like this:
IList<string> oIList1 = new List<string>{"1","2","3"};
IList<string> oIList2 = new List<string>{"4","5","6"};
IList<string> oIList3 = oIList1.Concat(oIList2).ToList();
So, basically you would use the Concat()
extension and ToList()
to get a similar functionality as AddRange()
.
Source