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

3
  • You're trying to get a [,]? Commented Mar 26, 2016 at 21:17
  • Do all lines have the same number of values? Commented Mar 26, 2016 at 21:19
  • Yes, all lines have the same number Commented Mar 26, 2016 at 21:23

2 Answers 2

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];
    }
}
Sign up to request clarification or add additional context in comments.

Comments

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.