Creating a List of Lists in C#

后端 未结 5 1083
梦谈多话
梦谈多话 2020-12-01 03:21

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

相关标签:
5条回答
  • 2020-12-01 03:54

    you should not use Nested List in List.

    List<List<T>> 
    

    is not legal, even if T were a defined type.

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

    0 讨论(0)
  • 2020-12-01 04:05

    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'?

    0 讨论(0)
  • 2020-12-01 04:12

    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 !

    0 讨论(0)
  • 2020-12-01 04:15

    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.

    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!

    //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");
    
    0 讨论(0)
  • 2020-12-01 04:16
    public class ListOfLists<T> : List<List<T>>
    {
    }
    
    
    var myList = new ListOfLists<string>();
    
    0 讨论(0)
提交回复
热议问题