API Proposal: Add typed convenience methods to WinForms collections
- Dominant language
- C#
- Stars
- 4.9k
- Forks
- 1.1k
- Avg merge
- 20h 23m
- Merged PRs (30d)
- 103
Description
### Background and motivation
WinForms "typed" collections predate generics. Many of them have not been updated with generic methods and interfaces to allow using things like LINQ, receiving typed collections, and efficient collection expressions.
### API Proposal
```diff
namespace System.Windows.Forms;
// IList throws on index set, don't want to add IList until we figure out the right way to deal with it
// (we could potentially remove the throws as you can perform the operations with multiple calls to
// existing ControlCollection APIs)
-public class ControlCollection : ArrangedElementCollection, IList, ICloneable
+public class ControlCollection : ArrangedElementCollection, IList, IEnumerable, ICloneable
{
public virtual void AddRange(params Control[] controls);
// Optimized internally to avoid converting to Control[] when we can avoid it.
+ public void AddRange(params ReadOnlySpan controls);
+ public void AddRange(IEnumerable controls);
+ IEnumerator IEnumerable.GetEnumerator();
}
public class ListView
{
- public class ListViewItemCollection : IList
- public class ListViewItemCollection : IList, IList
{
+ void IList.Insert(int index, ListViewItem item);
+ void ICollection.Add(ListViewItem item) => Add(item);
+ public void CopyTo(ListViewItem[] array, int arrayIndex);
+ bool ICollection.Remove(ListViewItem item);
+ IEnumerator IEnumerable.GetEnumerator()
public void AddRange(params ListViewItem[] items);
+ public void AddRange(params ReadOnlySpan items)
+ public void AddRange(IEnumerable collection)
}
}
```
There are a number of other collections which we'll bring back for API review, but the general approach would be as follows:
1. No new virtual methods
2. Add `AddRange` methods of `IEnumerable` and `params ReadOnlySpan`
3. IList -> IList, IList where plausible, at a minimum adding `IEnumerable`
### API Usage
```csharp
List controls = [];
control.Controls.AddRange(controls);
control.Controls.AddRange(control1, control2, control3);
```
### Will this feature affect UI controls?
Won't impact design scenarios.
Contributor guide
Assessment
This issue has not been assessed yet.