C# .NET - ItemCollection 在作为 IList<type> 传递时不编译

标签 c# .net collections types

我无法让代码片段 #2 工作,所以我不得不将我的逻辑更改为代码片段 #1。有人可以解释为什么代码片段 #2 不起作用吗?我认为它会,因为 ItemCollection 实现了 IList 接口(interface)......我知道存储在每个列表框中的类型(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);

最佳答案

ItemCollection不执行 IList<string> .

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

关于C# .NET - ItemCollection 在作为 IList<type> 传递时不编译,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4481580/

相关文章:

c++ - C++ 中 vector<double> 的 ArgMin?

java - 为什么在 java.util.Collections 中声明静态 java.util.Collections.fill() 方法而不是在 java.util.AbstractList 中声明实例方法?

c# - 绑定(bind)图表系列

c# - 实现一个接口(interface)但具有不同的成员名称

.net - 将 .exe 部署在与 .pdb 不同的位置

c# - 为什么 finally block 中的代码不执行?

java - 将列表元素转换为字符串

c# - ASP.NET ajax c# 总是返回未定义

c# - 为 WPF UserControl 加载后会发生什么事件?

c# - 序列化并添加到 XDocument