Checking the size of an array list

I have a list of string arrays:

List<string[]> parsedRaw = new List<string[]>();

This list contains lines read from the CSV, where parsedRaw [3] [5] will be the fifth item read from the third line of the CSV.

I know that I can find the number of rows in a list with:

parsedRaw.Count

But, given the row, how can I find the number of elements in this row? I am trying to run a test before entering a loop to read from a list to avoid the error "Index was outside the array", where the loop:

for (k = 0; k < nBytes; k++)
{
    TheseBytes[k] = (byte)parsedRaw[i][StartInt + k];
}

I ran into a line error in CSV that contains fewer elements than others. Before introducing this loop, I need to check if parsedRaw [i] has at least β€œStartInt + nBytes” elements.

Thanks for any suggestions!

+3
source share
3

string[], , Length .

foreach (string[] row in parsedRaw) {
    for (int i = 0 ; i != row.Length ; i++) {
        // do something with row[i]
    }
}
+6

parsedRaw[theRowIndex].Length

for, StartInt + k nBytes

for (k = 0; (k < nBytes) && (k + StartInt < parsedRaw[i].Length); k++)
{
    TheseBytes[k] = (byte)parsedRaw[i][StartInt + k];
}
+3

Try

List<string[]> parsedRaw = new List<string[]>();
parsedRaw.Add(new string[] {"test1", "test2"});
parsedRaw.Add(new string[] { "test1", "test2", "test3" });
int totalSize = 0;
for (int i = 0; i < parsedRaw.Count(); i++)
{
    int rowSize = 0;
    for (int k = 0; k < parsedRaw[i].Count(); k++)
    {
        rowSize += parsedRaw[i][k].Length;    
    }                
    totalSize += rowSize;
}
+1
source

All Articles