Cysharp / ObservableCollections

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

ObservableList.AddRange(IEnumerable) adds nulls due to a bug in CloneCollection #38

Closed jphorv-bdo closed 3 weeks ago

jphorv-bdo commented 2 months ago

I discovered this while using ObvervableList.AddRange(IEnumerable<T> items), which uses CloneCollection to turn the IEnumerable into a known-length collection as efficiently as possible.

In CloneCollection there is a bug inside the filling loop when it calls TryEnsureCapacity(ref array, i) on the array being filled.

Here TryEnsureCapacity() allocates a larger array without copying the old array items into the new one. As a result, the previous array contents are lost and are null in the final result.

Workaround: it works if I call ToArray() on my IEnumerable so that it will call ObservableList.AddRange(T[] items) instead.

jphorv-bdo commented 3 weeks ago

Confirmed fixed by PR #43 , thank you!