【问题标题】:Creating a List of Lists in C#在 C# 中创建列表列表
【发布时间】:2012-09-19 15:25:40
【问题描述】:

我似乎很难理解 C# 中通用列表的通用列表的想法。我认为问题源于<T> 参数的使用,我之前没有使用过该参数。有人可以提供一个简短的示例来声明一个 List 类,其中包含另一个 List,但其中包含的对象的类型不是立即知道的吗?

我一直在阅读有关泛型的 MS 文档,但我不确定是否可以声明 List<List<T>>,也不知道如何准确地将 <T> 参数传递给内部列表。

编辑:添加信息

在这里声明List<List<T>> 是否合法?如果您想知道,我正在构建一个类,它允许我使用ulong 作为索引器,并且(希望)通过维护一个列表来绕过.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>>();
        } 

【问题讨论】:

    标签: c# .net


    【解决方案1】:

    一个简单的例子:

    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);
        }
    }
    

    这就是你要找的吗?或者您是否尝试创建一个扩展 List&lt;T&gt; 的新 class,其成员为“列表”?

    【讨论】:

    • 这个答案完全没有抓住重点。 OP 想要声明一个列表列表,其中内部列表是通用的。本质上,OP 正在寻找一个变体列表...
    【解决方案2】:

    或者这个例子,只是为了让它更明显:

    public class CustomerListList : List<CustomerList> { }  
    
    public class CustomerList : List<Customer> { }
    
    public class Customer
    {
       public int ID { get; set; }
       public string SomethingWithText { get; set; }
    }
    

    你可以继续下去。到无限和超越!

    【讨论】:

    • +1。好主意。您甚至可以向其中添加客户特定的内容,例如将客户名称作为参数的Add 方法,它会自动创建和添加客户。 (为此,您需要一个 where T : new() 约束)。
    • 你可以只使用 alias',因为类的主体都是空的
    • @Servy,一个,我改变了帖子,让看到它的人尽可能清楚,tks
    • @ThePoet 在无限之外还有什么?
    【解决方案3】:
    public class ListOfLists<T> : List<List<T>>
    {
    }
    
    
    var myList = new ListOfLists<string>();
    

    【讨论】:

      【解决方案4】:

      我也一直在玩弄这个想法,但我试图实现一种稍微不同的行为。我的想法是创建一个继承自身的列表,从而创建一个数据结构,该数据结构本质上允许您在列表中的列表中嵌入列表中的列表...无限!

      实施

      //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 是一个泛型类型参数。它可以确保您班级的类型安全。创建 InfiniteList 的实例时,将 T 替换为您希望填充列表的类型,或者在此实例中,替换为 Value 属性的类型。

      示例

      //The InfiniteList.Value property will be of type string
      InfiniteList<string> list = new InfiniteList<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");
      

      【讨论】:

        【解决方案5】:

        你不应该在列表中使用嵌套列表。

        List<List<T>> 
        

        不合法,即使 T 是已定义的类型。

        https://msdn.microsoft.com/en-us/library/ms182144.aspx

        【讨论】:

        • 不合法是什么意思?当然如此。你链接到的只是一个设计警告。最好避免使用这种语法,特别是在公共 API 中,但有一些有效的用例可以做这些事情。
        【解决方案6】:

        我有以下代码

          public class ChildClass
            {
                public string FieldName { get; set; }
                public string Value { get; set; }
                public string StatusClass { get; set; }
                public string StatusMessage { get; set; }
            }
        

        创建list obj的列表如下

        List<List<ChildClass>> obj = new List<List<ChildClass>>();
        

        【讨论】:

          【解决方案7】:

          在这里看一个直接的例子:

          public partial class Form1 : Form
          {
              List<List<Label>> txtList;
              List<List<int>> num;
              public Form1()
              {
                  InitializeComponent();
                  txtList = new List< List<Label> >() { 
                      new List<Label> { label1, label2, label3 }, 
                      new List<Label> { label4, label5, label6 }, 
                      new List<Label> { label7, label8, label9 } 
                  };
          
                  num = new List<List<int>>() { new List<int>() { 1, 2 }, new List<int>() { 3, 4 } };
              }
          }
          

          【讨论】:

            猜你喜欢
            • 2018-06-25
            • 1970-01-01
            • 1970-01-01
            • 2016-02-04
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2013-05-27
            相关资源
            最近更新 更多