dotnet / dotnet/winforms

ListBox.IntegerCollection should contain unique values, but if we use the item setter, we can have duplicated

Open
#3,093 7 comments 0 reactions 0 assignees View on GitHub
:beetle: bug design-discussion
Dominant language
C#
Stars
4.9k
Forks
1.1k
Avg merge
20h 23m
Merged PRs (30d)
103

Description

* .NET Core Version:
Master

* Have you experienced this same bug with .NET Framework?:
Yes

**Problem description:**
`ListBox.IntegerCollection` should be a collection of unique integers. However, if we call the `collection[index] = valueAlreadyInCollection` we can end up with duplicates - see repro

**Expected behavior:**

We should not add the duplicate. We should also sort the array

**Minimal repro:**

Notice that the collection contains duplicates and is not sorted
```cs
[WinFormsFact]
public void ListBoxIntegerCollection_IListItem_Set_ReturnsExpected()
{
using var owner = new ListBox();
IList collection = new ListBox.IntegerCollection(owner);
collection.Add(2);
collection.Add(1);
collection.Add(1);
collection.Add(3);

// Set first.
collection[0] = 4;
Assert.Equal(new int[] { 4, 2, 3 }, collection.Cast());
Assert.Empty(owner.CustomTabOffsets);
Assert.False(owner.IsHandleCreated);

// Set middle.
collection[1] = 1;
Assert.Equal(new int[] { 4, 1, 3 }, collection.Cast());
Assert.Empty(owner.CustomTabOffsets);
Assert.False(owner.IsHandleCreated);

// Set last.
collection[2] = 4;
Assert.Equal(new int[] { 4, 1, 4 }, collection.Cast());
Assert.Empty(owner.CustomTabOffsets);
Assert.False(owner.IsHandleCreated);
}
```

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.