Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating a List of Lists in C#

Tags:

c#

.net

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 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>>();         }  
like image 325
user978122 Avatar asked Sep 27 '12 18:09

user978122


People also ask

Can you create a list in C?

There is no such thing as a list in C.

Can I make a list of lists in C#?

A simple solution for constucting a List of Lists is to create the individual lists and use the List<T>. Add(T) method to add them to the main list. The following example demonstrates its usage. That's all about creating a List of Lists in C#.

How do I make a list in C++ list?

In c++, what is the recommended way of creating a list of a certain size, where each element of the list is a list of 3 elements? If 3 is fixed at compile time, then you can use std::array<T, 3> as the outer container's element type.

What is list in C programming?

A linked list is a sequence of data structures, which are connected together via links. Linked List is a sequence of links which contains items. Each link contains a connection to another link. Linked list is the second most-used data structure after array.


1 Answers

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

like image 89
Gromer Avatar answered Oct 07 '22 10:10

Gromer