2

I'm working with CSV but all the tutorials I've read use 2D Lists.

private void cargaCSV()
    {
        List<string[]> values = new List<string[]>();

        var reader = new StreamReader(File.OpenRead(*my file*));
        while (!reader.EndOfStream)
        {
            string line = reader.ReadLine();
            values.Add(line.Split(';'));
        }
    }

My problem is my project works with 2D String Array.

I tried the following:

    string [,]  Data = values.ToArray();

I want to convert the 2d list into 2d array

Philip Kirkbride
  • 21,381
  • 38
  • 125
  • 225
Josema Pereira
  • 101
  • 1
  • 8

2 Answers2

2

If all the arrays have the same length, then you can do what you are doing and, after that, create and fill the array manually:

string[,] stringArray = new string[values.Count, values.First().Length]

for (int i = 0; i < values.Count; i++)
    row = values[i];
    for (int j = 0; j < row.Length; j++)
        string[i,j] = row[j];
    }
}
heltonbiker
  • 26,657
  • 28
  • 137
  • 252
1

You don't have to typecast hard and old way.

Simply replace

string [,]  Data = values.ToArray();

With

var  Data = values.ToArray();

Now Data is two dimensional array of strings.

Saleem
  • 8,728
  • 2
  • 20
  • 34