Siva S Siva S - 3 months ago 26
C# Question

Cannot convert from 'string' to 'system.collections.generic.list string'

I have the two list


  1. nested list of string, and

  2. list in string



In
index
list, I want to add
linesOfContent
with a
common
value and in-between i want to add separate string
":"
.

For that i write a code, but, I face a problem "cannot convert from 'string' to 'system.collections.generic.list string'". How to solve this.

int common = 10;
List<List<string>> index = new List<List<string>>();
List<int> linesOfContent = new List<int>();
for(int i = 0; i < 5; i++)
{
for(int j = 0; j < 5; j++)
{
linesOfContent.Add(i+":"+common);
}
index.Add(linesOfContent);
}


Expected Output:

index[0][0] = 0:10
index[0][1] = 1:10
index[0][2] = 2:10


...
...

Answer

A List of Lists of string should contain Lists of string, not Lists of int.

int common = 10;
List<List<string>> index = new List<List<string>>();
List<string> linesOfContent = new List<string>();
for(int i = 0; i < 5; i++)
{
    for(int j = 0; j < 5; j++)
    {       
        linesOfContent.Add(i.ToString() +":"+common.ToString());
    }
    index.Add(linesOfContent);
}
Comments