How to Declare a Multidimensional List in C#

Muhammad Maisam Abbas Feb 16, 2024
  1. Declare a 2D List With List<List<T>> in C#
  2. Declare a Multidimensional List With List of Classes in C#
  3. Declare a Multidimensional List With List of Tuples in C#
How to Declare a Multidimensional List in C#

In this tutorial, we will discuss methods to declare a multidimensional list in C#.

Declare a 2D List With List<List<T>> in C#

Unfortunately, there is no built-in method to declare a multidimensional list in C#. So, to achieve this objective, we have to rely on some user-defined approaches. We can declare a list of lists with the List<List<T>> notation in C#, where T specifies the type of variables inside the list. The following code example shows us how we can declare a 2D list with the List<List<T>> notation in C#.

using System;
using System.Collections.Generic;

namespace multidimensional_list {
  class Program {
    static void Main(string[] args) {
      List<List<string>> Person = new List<List<string>>();
      for (int i = 0; i < 3; i++) {
        List<string> Data = new List<string>();
        Data.Add("Person ");
        Data.Add("Description ");
        Person.Add(Data);
      }
      foreach (var list in Person) {
        foreach (var element in list) {
          Console.Write(element);
        }
        Console.WriteLine();
      }
    }
  }

Output:

Person Description
Person Description
Person Description

We declared a 2D list Person of string variables with the List<List<string>> notation in C#. To input data in the list, we have to store the data into another list of the string variables and then add that object to the Person list with the Person.Add() function. We output the list with a nested loop. This method’s drawback is that it only works for 2D arrays, and we can only have elements of 1 data type in it.

Declare a Multidimensional List With List of Classes in C#

If we want to create a list of multiple data types, we can declare a list of class objects in C#. As we know, a class can have multiple variables of different data types in it. The following code example shows us how we can declare a 2D list by creating a list of objects in C#.

using System;
using System.Collections.Generic;

namespace multidimensional_list {
  public class Person {
    public string Name { get; set; }
    public string Description { get; set; }
  }
  class Program {
    static void Main(string[] args) {
      List<Person> People = new List<Person>();
      for (int i = 0; i < 3; i++) {
        Person p = new Person();
        p.Name = "Person ";
        p.Description = "Description ";
        People.Add(p);
      }
      foreach (var obj in People) {
        Console.WriteLine(obj.Name);
        Console.WriteLine(obj.Description);
      }
    }
  }

Output:

Person
Description
Person
Description
Person
Description

We declared a 2D list People by creating a list of Person class in C#. To input data in the list, we have to store the data into an object of the Person class and then add that object to the People list with the People.Add() function. With this approach, we can have multiple elements of different data types in the list.

Declare a Multidimensional List With List of Tuples in C#

If we want to use multiple data types in our list but don’t want to create a separate class to store all the variables, we can also declare a list of tuples. We can declare a list of tuples with the List<(T1, T2)> notation in C#, where T1 and T2 are the data types of the first and second element of the tuple, respectively.

The following code example shows us how to declare a multidimensional list with a list of tuples in C#.

using System;
using System.Collections.Generic;

namespace multidimensional_list {
  class Program {
    static void Main(string[] args) {
      List<(string, string)> Person = new List<(string, string)>();
      for (int i = 0; i < 3; i++) {
        Person.Add(("Person ", "Description "));
      }
      foreach (var list in Person) {
        Console.WriteLine(list);
      }
    }
  }

Output:

(Person , Description )
(Person , Description )
(Person , Description )

We declared a list of tuples Person with the List<(string, string)> notation in C#. We input data in the Person list in the form of tuples and display it with a foreach loop.

Muhammad Maisam Abbas avatar Muhammad Maisam Abbas avatar

Maisam is a highly skilled and motivated Data Scientist. He has over 4 years of experience with Python programming language. He loves solving complex problems and sharing his results on the internet.

LinkedIn

Related Article - Csharp List