简体   繁体   English

IList的C#参数 <IList<(ClassType)> &gt;

[英]C# Parameter of IList<IList<(ClassType)>>

I can't seem to figure out why the following doesn't work. 我似乎无法弄清楚以下原因为何不起作用。

From reading around it seems it must be somehow related to something like this . 从周围的阅读看来,这一定与这种事情有关。

public class Test<T>
{
    public void SomeFunction(IList<IList<T>> ListOfLists)
    {
    }
}

....

List<List<SimpleClass>> lists = new List<List<SimpleClass>>();
new Test<SimpleClass>().SomeFunction( (IList<IList<SimpleClass>>)lists );

How can I require a list to actually be a list of lists? 如何要求列表实际上是列表列表? I can't seem to grasp what I am doing wrong. 我似乎无法理解我做错了什么。

Thanks, 谢谢,

A List<List<SimpleClass>> isn't an IList<IList<SimpleClass>> . List<List<SimpleClass>>不是IList<IList<SimpleClass>>

For example, for the latter type, you could call lists.Add(new SimpleClass[5]) as SimpleClass[] implements IList<SimpleClass> . 例如,对于后一种类型,可以调用lists.Add(new SimpleClass[5])因为SimpleClass[]实现IList<SimpleClass> But you can't do that with a List<List<SimpleClass>> , because each element has to be a List<SimpleClass> . 但是您不能使用List<List<SimpleClass>>来做到这一点,因为每个元素都必须是List<SimpleClass>

This would be okay though: 不过,这可以:

List<IList<SimpleClass>> lists = new List<IList<SimpleClass>>();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM