在 C# 中创建列表列表
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12628222/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Creating a List of Lists in C#
提问by user978122
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?
我似乎在围绕 C# 中泛型列表的泛型列表的想法进行思考时遇到了一些麻烦。我认为问题源于<T>参数的使用,我之前没有使用过。有人可以提供一个简短的例子来声明一个类是一个列表,其中包含另一个列表,但其中包含的对象的类型不是立即知道的吗?
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.
我一直在阅读有关泛型的 MS 文档,但我不确定是否可以声明 a List<List<T>>,也不确定如何将<T>参数传递给内部列表。
Edit: Adding information
编辑:添加信息
Would declaring a List<List<T>>be considered legal here? In case you are wondering, I am building a class that allows me to use a ulongas the indexer, and (hopefully) steps around the nasty 2GB limit of .Net by maintaining a List of Lists.
List<List<T>>在这里声明 a会被认为是合法的吗?如果您想知道,我正在构建一个类,允许我使用 aulong作为索引器,并且(希望)通过维护列表列表来绕过 .Net 令人讨厌的 2GB 限制。
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>>();
}
回答by Gromer
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 classthat extends List<T>that has a member that is a `List'?
这就是你要找的吗?或者您是否正在尝试创建一个class扩展的新List<T>成员,该成员具有一个“列表”?
回答by RollRoll
or this example, just to make it more visible:
或者这个例子,只是为了让它更明显:
public class CustomerListList : List<CustomerList> { }
public class CustomerList : List<Customer> { }
public class Customer
{
public int ID { get; set; }
public string SomethingWithText { get; set; }
}
and you can keep it going. to the infinity and beyond !
你可以继续下去。前无古人后无来者 !
回答by Michael Christensen
public class ListOfLists<T> : List<List<T>>
{
}
var myList = new ListOfLists<string>();
回答by Matthew Layton
I have been toying with this idea too, but I was trying to achieve a slightly different behavior. My idea was to make a list which inherits itself, thus creating a data structure that by nature allows you to embed lists within lists within lists within lists...infinitely!
我也一直在玩这个想法,但我试图实现一种稍微不同的行为。我的想法是制作一个继承自身的列表,从而创建一个数据结构,本质上允许您将列表嵌入列表中的列表中......无限!
Implementation
执行
//InfiniteList<T> is a list of itself...
public class InfiniteList<T> : List<InfiniteList<T>>
{
//This is necessary to allow your lists to store values (of type T).
public T Value { set; get; }
}
T is a generic type parameter. It is there to ensure type safety in your class. When you create an instance of InfiniteList, you replace T with the type you want your list to be populated with, or in this instance, the type of the Value property.
T 是泛型类型参数。它是为了确保您班级的类型安全。当您创建 InfiniteList 的实例时,您将 T 替换为您希望填充列表的类型,或者在此实例中,使用 Value 属性的类型。
Example
例子
//The InfiniteList.Value property will be of type string
InfiniteList<string> list = new InfiniteList<string>();
A "working" example of this, where T is in itself, a List of type string!
一个“工作”示例,其中 T 本身就是一个字符串类型的列表!
//Create an instance of InfiniteList where T is List<string>
InfiniteList<List<string>> list = new InfiniteList<List<string>>();
//Add a new instance of InfiniteList<List<string>> to "list" instance.
list.Add(new InfiniteList<List<string>>());
//access the first element of "list". Access the Value property, and add a new string to it.
list[0].Value.Add("Hello World");
回答by freakydinde
you should not use Nested List in List.
您不应该在列表中使用嵌套列表。
List<List<T>>
is not legal, even if T were a defined type.
不合法,即使 T 是定义的类型。

