C# .NET - ItemCollection 作为 IList传递时未编译

发布于 2024-10-08 15:28:04 字数 1789 浏览 1 评论 0原文

我无法让代码片段#2 工作,所以我必须将逻辑更改为代码片段#1。有人可以解释一下为什么代码片段 #2 不起作用吗?我认为会的,因为 ItemCollection 实现了 IList 接口...我知道每个列表框中存储的类型(listbox1 是字符串,listbox2 是 PictureInfo 类型)。

(注:PictureInfo是我自己定义的类型)

1:

    private void MoveImagesFromHCollectionToCollection(ItemCollection collectionMoveFrom, ItemCollection collectionMoveTo)
            {
                //Loop through All strings in the lstAll list box.  Then use each picture path to convert 
                //each picture into their own class
                foreach (string file in collectionMoveFrom)
                {
                    PictureInfo mp = ReturnPictureInfoTypeBasedOffFileExtension(img, file);
                    collectionMoveTo.Add(mp);
                    collectionMoveFrom.Remove(file);                         
                    //...
                }
            }

//Method call...
MoveImagesFromHCollectionToCollection(listbox1.Items, listbox2.Items);

2:

private void MoveImagesFromHCollectionToCollection(IList<string> collectionMoveFrom, IList<PictureInfo> collectionMoveTo)
        {
            //Loop through All strings in the lstAll list box.  Then use each picture path to convert 
            //each picture into their own class
            foreach (string file in collectionMoveFrom)
            {

                 PictureInfo mp = ReturnPictureInfoTypeBasedOffFileExtension(img, file);

                 collectionMoveTo.Add(mp);
                 collectionMoveFrom.Remove(file);
                 //...
            }
        }

//Method call...parameter 1 Listbox contains strings, parameter 2 Listbox contains objects of type PictureInfo
MoveImagesFromHCollectionToCollection(listbox1.Items, listbox2.Items);

I couldn't get code snippet #2 to work, so I had to change my logic to code snippet #1. Can someone please explain why code snippet #2 wouldn't work? I thought it would, since ItemCollection implements the IList interface... I know the types that are stored in each listbox (listbox1 is string, and listbox2 is PictureInfo type).

(Note: PictureInfo is my own defined type)

1:

    private void MoveImagesFromHCollectionToCollection(ItemCollection collectionMoveFrom, ItemCollection collectionMoveTo)
            {
                //Loop through All strings in the lstAll list box.  Then use each picture path to convert 
                //each picture into their own class
                foreach (string file in collectionMoveFrom)
                {
                    PictureInfo mp = ReturnPictureInfoTypeBasedOffFileExtension(img, file);
                    collectionMoveTo.Add(mp);
                    collectionMoveFrom.Remove(file);                         
                    //...
                }
            }

//Method call...
MoveImagesFromHCollectionToCollection(listbox1.Items, listbox2.Items);

2:

private void MoveImagesFromHCollectionToCollection(IList<string> collectionMoveFrom, IList<PictureInfo> collectionMoveTo)
        {
            //Loop through All strings in the lstAll list box.  Then use each picture path to convert 
            //each picture into their own class
            foreach (string file in collectionMoveFrom)
            {

                 PictureInfo mp = ReturnPictureInfoTypeBasedOffFileExtension(img, file);

                 collectionMoveTo.Add(mp);
                 collectionMoveFrom.Remove(file);
                 //...
            }
        }

//Method call...parameter 1 Listbox contains strings, parameter 2 Listbox contains objects of type PictureInfo
MoveImagesFromHCollectionToCollection(listbox1.Items, listbox2.Items);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

青衫儰鉨ミ守葔 2024-10-15 15:28:05

ItemCollection 未实现 IList

IList(非通用)是一个不同的接口。

ItemCollection does not implement IList<string>.

IList (non-generic) is a different interface.

筑梦 2024-10-15 15:28:05

它继承自 IList 接口,但不继承 IList 接口。它们是不同的,因为后者接受特定类型的对象。

IList 文档

IList< T>文档

It inherits from the IList interface but not the IList<T> interface. They are different because the latter accepts a specific type of object.

IList Documentation

IList< T > Documentation

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文