检查数组列表的大小

Pat*_*gon 3 c# list indexoutofboundsexception

我有一个字符串数组列表:

List<string[]> parsedRaw = new List<string[]>();
Run Code Online (Sandbox Code Playgroud)

此列表包含从CSV读入的行,其中parsedRaw [3] [5]将是从CSV的第三行读取的第五个项目.

我知道我可以在列表中找到行数:

parsedRaw.Count
Run Code Online (Sandbox Code Playgroud)

但是,给定一行,如何找到该行中的元素数量?我正在尝试在进入循环以从列表中读取之前实现测试,以避免"索引超出数组的范围"错误,其中循环是:

for (k = 0; k < nBytes; k++)
{
    TheseBytes[k] = (byte)parsedRaw[i][StartInt + k];
}
Run Code Online (Sandbox Code Playgroud)

我遇到了CSV中一行中的错误,其中元素少于其他元素.在进入此循环之前,我需要检查parsedRaw [i]是否至少具有"StartInt + nBytes"元素.

谢谢你的任何建议!

das*_*ght 6

行只是一个字符串数组string[],因此您可以使用Length数组的属性查找其大小.

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