首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >IndexOutOfRange on GetFiles

IndexOutOfRange on GetFiles
EN

Stack Overflow用户
提问于 2018-07-11 08:59:45
回答 2查看 323关注 0票数 0

我试图阅读大量的文件,并将一些信息存储在字典中。我的完整代码是:

代码语言:javascript
复制
[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);
}

string[] fileList = System.IO.Directory.GetFiles("C:\\mySQL");引发一个异常:

代码语言:javascript
复制
Exception thrown: 'System.IndexOutOfRangeException' in XXXXX.dll: 'Index was outside the bounds of the array.'

如果我对CodeMappingX赋值进行注释,则没有错误,并且填充了fileList。我不明白前几行为什么会影响到这一行。有人能向我解释原因吗?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2018-07-11 09:03:55

好的,我在提交时找到了解决方案,它显然是Visual的一个错误,在调试模式中没有指向正确的行。我的第一句话应该是:

代码语言:javascript
复制
Dictionary<string, List<Tuple<string, ushort>>>[] CodeMapping = new Dictionary<string, List<Tuple<string, ushort>>>[257];
票数 0
EN

Stack Overflow用户

发布于 2018-07-11 09:31:37

我认为您应该在n-1位置上创建字典,因为如果集合了256个元素,则最大索引将从0到255。

所以你把这个改写成:

代码语言:javascript
复制
/* Pre-creating some dictionaries */
    CodeMapping[1] = new  Dictionary<string, List<Tuple<string, ushort>>>(256);
    CodeMapping[7] = new  Dictionary<string, List<Tuple<string, ushort>>>(256);
    CodeMapping[15] = new  Dictionary<string, List<Tuple<string, ushort>>>(256);
    CodeMapping[31] = new  Dictionary<string, List<Tuple<string, ushort>>>(256);
    CodeMapping[63] = new  Dictionary<string, List<Tuple<string, ushort>>>(256);
    CodeMapping[127] = new  Dictionary<string, List<Tuple<string, ushort>>>(256);
    CodeMapping[255] = new  Dictionary<string, List<Tuple<string, ushort>>>(256);
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51281318

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档