ObservableCollections icon indicating copy to clipboard operation
ObservableCollections copied to clipboard

A zipped sequence causes an exception in ObservableList.AddRange.

Open yossiepon opened this issue 1 year ago • 1 comments

I am getting an exception with the code below.

using ObservableCollections;

var seq1 = Enumerable.Range(0, 1);
var seq2 = Enumerable.Range(0, 1);
var zippedSeq = seq1.Zip(seq2, (num1, num2) => (num1, num2));

var list = new ObservableList<(int,int)>();
list.AddRange(zippedSeq); // IndexOutOfRangeException raised.

I think there's something wrong with the CloneCollection construction process when the enumerator doesn't support NonEnumeratedCount.

CloneCollection.TryEnsureCapacity seems to be returning an empty array.

yossiepon avatar Aug 24 '22 20:08 yossiepon

Hi, I remember falling int the same issue and if i recall correctly the reason for the exception was mapping the input sequence onto a Span. The fix is to materialize the sequence first, for example with .ToList() before calling AddRange():

list.AddRange(zippedSeq.ToList());

slimshader avatar Aug 25 '22 06:08 slimshader