Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,10 @@ public void RemoveAt(int index)
{
Array.Copy(_items, index + 1, _items, index, _size - index);
}
_items[_size] = default(T)!;
if (RuntimeHelpers.IsReferenceOrContainsReferences<T>())
{
_items[_size] = default(T)!;
}
_version++;
}

Expand Down Expand Up @@ -204,7 +207,10 @@ public void Clear()
{
if (_size > 0)
{
Array.Clear(_items, 0, _size);
if (RuntimeHelpers.IsReferenceOrContainsReferences<T>())
{
Array.Clear(_items, 0, _size);
}
_size = 0;
}
_version++;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -518,6 +518,21 @@ public void ReadOnlyCollectionBuilder_Clear2()
Assert.Equal(0, rocb.Count);
}

[Fact]
public void Clear_AfterRemoval_DoesNotExposeOldItems()
{
var builder = new ReadOnlyCollectionBuilder<int>(new[] { 1, 2, 3 });
builder.RemoveAt(1);

builder.Clear();
Assert.Empty(builder);

builder.Add(4);
builder.Insert(0, 5);
Assert.Equal(new[] { 5, 4 }, builder);
Assert.Equal(new[] { 5, 4 }, builder.ToReadOnlyCollection());
}

[Fact]
public void ReadOnlyCollectionBuilder_Contains1()
{
Expand Down