views:

109

answers:

5

I would like to create a list inside another list. How can I do this?

And how do I retrieve values from the list which is inside another list?

+9  A: 

I'm not sure if I correctly understand your question. Do you want something like this?

var listOfList = new List<List<int>>(); 
listOfList.Add(new List<int>());
listOfList[0].Add(42);
Mehrdad Afshari
thank you for ur answer .ya need like this only i need more clear answer
ratty
If you want a clearer answer, ask a clearer question.
Sapph
What do you mean, you need a 'more clear answer' ? If it's not clear enough, you should start off with asking a better formulated question.
Frederik Gheysels
I'm not sure how else I can clarify this. You are creating a list of `List<int>`. The items in the outer list are `List<int>` objects. In the first line, you create the outer list. In the second line, you create a list of int and add it as one of the items in the outer list. In the third line, you add an integer to the first inner list in the outer list.
Mehrdad Afshari
+3  A: 

Something like this, you mean:

List<List<int>> someList = new List<List<int>>();

This is a List of Lists

Frederik Gheysels
+1  A: 

If you want strong type lists of for example ints:

 var list = new List<List<int>>();

of for none strong type lists:

new List<List<object>>();
Hans van Dodewaard
A: 

pfuh ... correct answers already thrown in ... just another style:

var mySpecialList = new List<List<int>>
{
    new List<int>
    {
        4
    },
    new List<int>
    {
        12,
        17
    }
}
Andreas Niedermair
The first line should read ` ... = new List<List<int>>`
Mehrdad Afshari
thank you ... fixed typo!
Andreas Niedermair
A: 

You could always create a custom class to contain your data e.g.

public class myContainer<T>
{
    public List<T> contents {get; set;}
}

List<myContainer> x = new List<myContainer>();

This has the benefit that if the data type in myContainer changes its isolated from code to get the sub-lists.

TK