C# Nested Lists: Create 2D List or Jagged List

This C# example program uses a nested List. Nested lists are like jagged or 2D lists.

List, nested.

A List can have elements of List type. This is a jagged list, similar in syntax to a jagged array. We can develop multidimensional Lists. We use an easily-developed and expandable data structure.


The List generic type in C# provides an underlying and internal array where it stores its elements. But it always specifies a one-dimensional and zero-based array in its internal structure.List

Note: When you create a List whose element type is another List, the first List will contain an internal array of List references.

And: The List objects these references point to are separate. They will contain one-dimensional arrays.

C# program that uses nested Lists using System; using System.Collections.Generic; class Program { static void Main() { // // Add rows and columns to the List. // List<List<int>> list = new List<List<int>>(); var rand = new Random(); for (int i = 0; i < 10; i++) { // // Put some integers in the inner lists. // List<int> sublist = new List<int>(); int top = rand.Next(1, 15); for (int v = 0; v < top; v++) { sublist.Add(rand.Next(1, 5)); } // // Add the sublist to the top-level List reference. // list.Add(sublist); } // // Display the List. // Display(list); } static void Display(List<List<int>> list) { // // Display everything in the List. // Console.WriteLine("Elements:"); foreach (var sublist in list) { foreach (var value in sublist) { Console.Write(value); Console.Write(' '); } Console.WriteLine(); } // // Display element at 3, 3. // if (list.Count > 3 && list[3].Count > 3) { Console.WriteLine("Element at 3, 3:"); Console.WriteLine(list[3][3]); } // // Display total count. // int count = 0; foreach (var sublist in list) { count += sublist.Count; } Console.WriteLine("Count:"); Console.WriteLine(count); } } Output Elements: 4 2 4 1 2 1 3 2 4 2 4 3 3 1 4 2 4 1 2 1 2 1 3 4 4 3 2 4 2 1 2 4 4 3 1 4 3 4 4 3 2 1 4 1 2 4 1 3 2 2 1 3 1 4 1 4 2 2 3 2 1 2 Element at 3, 3: 1 Count: 62

In this example,

the List type signature used has confusing syntax because it must specify nested type parameters. You can sometimes simplify this syntax by using the implicit typing provided by the var-keyword.Var

So: The Main method first creates a List instance. It then populates it with ten new List instances as its elements.

And: Those latter List instances are initialized with a random number of random integers.

RandomInt, uint

The Display method

receives a parameter of type List<List<int>>. In the method signature, you must type out the entire type exactly. Keep in mind that nested List types with specific inner type parameters are unique.

Tip: The Display method uses the foreach-loop syntax to loop over the inner contents of each List.



the Display method also shows how to access a specific element such as one at indexes {3, 3} in the 2-dimensional List type. You must be careful to check the Count property on each List separately.

Finally: The example counts all the elements together and displays that figure with Console.WriteLine.



A nested List type is relatively fast. It internally uses zero-based arrays, and the List type itself is fast. But adding another layer of abstraction and creating a nested class may be desirable.

Note: This can help reduce the amount of complexity and syntax confusion at the calling sites.

Tip: Another option is to encapsulate the List<List<T>> into a separate class. This could be better for real-world programs.



We used a nested List that is essentially a jagged or 2D List data type with the generic List class. The code here is not ideal but is fine for prototyping or experimental work.Jagged Arrays
Dot Net Perls
© 2007-2019 Sam Allen. All rights reserved. Written by Sam Allen,