Cysharp / ObservableCollections

High performance observable collections and synchronized views, for WPF, Blazor, Unity.
MIT License
544 stars 43 forks source link

A zipped sequence causes an exception in ObservableList.AddRange. #11

Closed yossiepon closed 1 year ago

yossiepon commented 2 years ago

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.

slimshader commented 2 years ago

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());
github-actions[bot] commented 1 year ago

This issue is stale because it has been open 90 days with no activity. Remove stale label or comment or this will be closed in 7 days.