My class implements IEnumerable<T>. Class also implements:
void AddRange(IEnumerable<T> items)
{
// items can be a deffered query against the collection items being added to
var itemslist = items.ToList();
...processing and adding...
}
But I have some doubts whether convertion to a static collection is a good design.
For example List itself has nothing like this:
var list = new List<int> {1, 2, 3};
list.AddRange(list.Select(i => i+list.Count).ToList());
No exception.
var list = new List<int> {1, 2, 3};
list.AddRange(list.Select(i => i+list.Count));
InvalidOperationException : collection was modified during enumeration.
List<T>.AddRangewill behave differently depending on whether the sequence it’s passed implementsICollection<T>. In the first case,List<T>implementsICollection<T>, soAddRangecan callCopyToto do a bulk copy. In the second you’re just passing in an arbitraryIEnumerable<T>, so it has to iterate over it in order to proceed.Fundamentally you’re doing something odd in the second case though. The simple answer is not to do that. I wouldn’t expect the results to be guaranteed – it should be up to the implementation whether it eagerly iterates over the passed in sequence and then copies it, or whether it does it lazily. Either implementation will work fine for any sane sequence.