创建列表在C#中的列表列表

2023-09-03 02:33:19 作者:越夜越有神

我似乎遇到了一些麻烦环绕我的头周围的泛型列表泛型列表在C#中的想法。我认为这个问题是源于使用了&LT的; T> 的说法,这是我之前没有任何经验的玩。可能有人提供声明类,它是一个列表,这其中包含了另一个列表的一个简单的例子,但在那里的对象所含的类型还不清楚?

我一直在读通过泛型的MS文档,我不能马上知道如果我可以声明名单,其中,名单< T>> ,也没怎么正是通过对< T> 参数内部列表

编辑:将信息添加

请问声明名单,其中,名单< T>> 在这里视为合法?如果你想知道,我建立一个类来,让我用一个ULONG作为索引,和(希望)周围的.Net的讨厌2GB的限制措施,通过维护列表的列表。

 公共类DynamicList64< T>
    {
        私人名单,其中,名单< T>>数据=新的名单,其中,名单< T>>();

        私人ULONG容量= 0;
        私人const int的maxnumberofitemsperlist = Int32.MaxValue;



        公共DynamicList64()
        {
            数据=新的名单,其中,名单< T>>();
        }
 

解决方案

一个简单的例子:

 名单,其中,名单,其中,串>> myList上=新的名单,其中,名单,其中,串>>();
myList.Add(新名单,其中,串> {A,B});
myList.Add(新名单,其中,串> {C,D,E});
myList.Add(新名单,其中,串> {QWERTY,自卫队,ZXCV});
myList.Add(新名单,其中,串> {A,B});

//遍历它。
的foreach(列表<字符串>子列表中myList中)
{
    的foreach(子列表字符串项)
    {
        Console.WriteLine(项目);
    }
}
 
新版乐鱼影音盒 音乐大放送

这就是你要找的东西?或者是你想创建一个新的扩展名单,其中,T> ,有一个成员是一个'清单'?

I seem to be having some trouble wrapping my head around the idea of a Generic List of Generic Lists in C#. I think the problem stems form the use of the <T> argument, which I have no prior experience playing with. Could someone provide a short example of declaring a class which is a List, that therein contains another List, but where the type of the object contained therein is not immediately known?

I've been reading through the MS documentation on Generics, and I am not immediately sure if I can declare a List<List<T>>, nor how exactly to pass the <T> parameter to the inside list.

Edit: Adding information

Would declaring a List<List<T>> be considered legal here? In case you are wondering, I am building a class to that allows me to use a ulong as the indexer, and (hopefully) steps around the nasty 2GB limit of .Net by maintaining a List of Lists.

public class DynamicList64<T>
    {
        private List<List<T>> data = new List<List<T>>();

        private ulong capacity = 0;
        private const int maxnumberofitemsperlist = Int32.MaxValue;



        public DynamicList64()
        {
            data = new List<List<T>>();
        } 

解决方案

A quick example:

List<List<string>> myList = new List<List<string>>();
myList.Add(new List<string> { "a", "b" });
myList.Add(new List<string> { "c", "d", "e" });
myList.Add(new List<string> { "qwerty", "asdf", "zxcv" });
myList.Add(new List<string> { "a", "b" });

// To iterate over it.
foreach (List<string> subList in myList)
{
    foreach (string item in subList)
    {
        Console.WriteLine(item);
    }
}

Is that what you were looking for? Or are you trying to create a new class that extends List<T> that has a member that is a `List'?