Search code examples
c#stringlistsublist

How to access unique sublist from another class


I have a list of list and I want to access for example the second sublist and add a string.

 public static List<List<string>> logsIP1 = new List<List<string>>();

 public static void logsList()
 {
        logsIP1.Add(new List<string> { });
        logsIP1.Add(new List<string> { });
        logsIP1.Add(new List<string> { });
 }

I want something like

Logs.logsIP1.Add(List<string>[0]("test");

Solution

  • Try something like:

     public static List<List<string>> logsIP1 = new List<List<string>>();
     public static void Add(int index, string value)
     {
        var nestedList = logsIP1[index];
        nestedList.Add(value);
      }
    

    Then you can use method Add() to insert new value into nested list by index of main list