Nested Generic Lists? Cool!

While answering a question on the Asp.Net forums, I was pleasantly surprised to find nested Generic Lists are possible. They seem much easier than multidimensional arrays.

Here is the key line:     List<List<int>> MyListOfIntLists; 

    protected void Button1_Click(object sender, EventArgs e)
    {
        List<int> MyInts1;
        List<int> MyInts2;
 
        List<List<int>> MyListOfIntLists;
 
        // create the list of lists
        MyListOfIntLists = new List<List<int>>();
 
        MyInts1 = new List<int>();
        MyInts1.Add(1);
        MyInts1.Add(2);
        MyInts1.Add(3);
 
        MyInts2 = new List<int>();
        MyInts2.Add(4);
        MyInts2.Add(5);
        MyInts2.Add(6);
 
        MyListOfIntLists.Add(MyInts1);
        MyListOfIntLists.Add(MyInts2);
 
        // access the list of lists
        MyInts1 = MyListOfIntLists[0];
        MyInts2 = MyListOfIntLists[1];
    }

 

I hope you find this useful.

Steve Wellens

6 Comments

Comments have been disabled for this content.