I am trying to read a large number of files and to store some information in a dictionary. My complete code is:
[HttpGet("[action]")]
public JsonResult GenerateMapFiles()
{
Dictionary<string, List<Tuple<string, ushort>>>[] CodeMapping = new Dictionary<string, List<Tuple<string, ushort>>>[256];
/* Pre-creating some dictionaries */
CodeMapping[2] = new Dictionary<string, List<Tuple<string, ushort>>>(256);
CodeMapping[8] = new Dictionary<string, List<Tuple<string, ushort>>>(256);
CodeMapping[16] = new Dictionary<string, List<Tuple<string, ushort>>>(256);
CodeMapping[32] = new Dictionary<string, List<Tuple<string, ushort>>>(256);
CodeMapping[64] = new Dictionary<string, List<Tuple<string, ushort>>>(256);
CodeMapping[128] = new Dictionary<string, List<Tuple<string, ushort>>>(256);
CodeMapping[256] = new Dictionary<string, List<Tuple<string, ushort>>>(256);
string[] fileList = System.IO.Directory.GetFiles("C:\\mySQL");
/* Processing code was here, but I commented it and it is still generating exception */
return Json(CodeMapping);
}
The line string[] fileList = System.IO.Directory.GetFiles("C:\\mySQL");
raises an exception:
Exception thrown: 'System.IndexOutOfRangeException' in XXXXX.dll: 'Index was outside the bounds of the array.'
If I comment the CodeMapping[X] assignments, there is no error and fileList is populated. I do not understand why the previous lines impact this one. Would someone be able to explain to me why?