I think there are two parts to this.
The first is that accepting an IEnumerable
is more flexible than accepting an array. What if the user has a List<DataRow>
, or some other collection type? What if they want to pass the result of a linq query? Accepting an array forces them to allocate a new array just to pass to CopyToDataTable
, and that's unnecessary cost.
There's no reason why CopyToDataTable
needs an array: it just needs an IEnumerable
. So it's best if it just accepts an IEnumerable
.
That said, due to covariance it would be possible to use the signature:
DataTable CopyToDataTable(this IEnumerable<DataRow> source)
... and users would be able to pass e.g. an IEnumerable<TableDetailsRow>
(where TableDetailsRow
extends DataRow
). However, covariance was only introduced in C# 4, and that method has been around since .NET 3.5, which means it was probably written in C# 3. Since covariance wasn't available then, generics was the next best thing.