Pavel Voronin
Pavel Voronin

Reputation: 14015

Should my IEnumerable enumerate IEnumerable which is passed as a parameter to method like AddRange()?

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.

Upvotes: 2

Views: 133

Answers (1)

Jon Skeet
Jon Skeet

Reputation: 1503389

List<T>.AddRange will behave differently depending on whether the sequence it's passed implements ICollection<T>. In the first case, List<T> implements ICollection<T>, so AddRange can call CopyTo to do a bulk copy. In the second you're just passing in an arbitrary IEnumerable<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.

Upvotes: 1

Related Questions