我正在尝试创建一个公共可访问的词典,以便我的整个程序都可以访问。我按照以下方式在我的表单下创建了它:
public static Dictionary<string, int> categories = new Dictionary<string, int>
{
{"services", 0},
{"files", 0},
{"shortcuts", 0},
{"registry", 0},
{"browsers", 0}
};
现在,在一个私有方法中,我有这段代码:
foreach (KeyValuePair<string, int> reference in categories)
{
for (int i = 0; i < scanLines.Count; i++)
{
if (scanLines[i].Contains(reference.Key))
{
start = i + 1;
break;
}
}
for (int i = start; i < scanLines.Count; i++)
{
if (scanLines[i].Contains("*"))
{
stop = i - 1;
break;
}
}
// Write the result for the category by subtracting the difference between
// the start and stop variables
categories[reference.Key] = stop - start;
这基本上是将日志文件分成部分,以计算类别字典中声明的这些部分之间的行数。现在我的问题是代码行:
categories[reference.Key] = stop - start;
程序一直抛出"无效操作异常"错误。这里我做错了什么?
.ToList
或.ToArray
时,您是否会遇到异常? - Ulugbek Umirov