Skip to content

ComboBox.ObjectCollection.CopyTo(Array destination, int index) copies Entry object, not inner item #14071

@abbweiman

Description

@abbweiman

.NET version

.NET 8, 10 etc.

Did it work in .NET Framework?

Yes

Did it work in any of the earlier releases of .NET Core or .NET 5+?

No response

Issue description

ComboBox.ObjectCollection.CopyTo(Array destination, int index) "leaks" the Entry objects to destination, instead of copying the actual items.

It should be InnerList[i].Item here:

cf. CopyTo(object[] destination, int arrayIndex):

And also for example ListBox.ObjectCollection.CopyTo(Array destination, int index):

Steps to reproduce

This code works in .NET 4.8 but throws InvalidCastException in .NET core.

        class MyItem
        {
        }
        static void Main(string[] args)
        {
            var cbo = new ComboBox();
            cbo.Items.Add(new MyItem());
            var item1 = (MyItem)cbo.Items[0];
            var arrayList = new ArrayList(cbo.Items);
            var item2 = (MyItem)arrayList[0]; // <-- InvalidCastException
        }

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions