如何解决:索引脱离绑定的C#数组、流[重复]。

问题描述 投票:0回答:1

我得到index out of bound on line: stream.Read(fileBytes[i], 0, fileBytes[i].Length);任何帮助将是感激的.谢谢:)

string[] path = new string[15];
byte[][] fileBytes = new byte[10][];
for (int i = 1; i <= 10; i++) {

 path[i] = @ "C:\Users\t-chkum\Desktop\InputFiles\1MB\" + i + ".txt ";

 // readind data/* 
 FileStream stream = File.OpenRead(path[i]);
 fileBytes[i] = new byte[stream.Length];

 //  Console.WriteLine(stream.Length);
 stream.Read(fileBytes[i], 0, fileBytes[i].Length);
 stream.Close();
}
c# arrays filestream
1个回答
3
投票

你正在迭代从 i=110 包罗万象。但是,数组的大小是 10 即指数从 09. 所以,要么开始 i0或使用 i-1. 另外,使用 try-catch. 这将有助于你更好地调试。

否则,代码对我来说是正常工作的。

© www.soinside.com 2019 - 2024. All rights reserved.