5

所以,我有这个从 INI 文件中读取的函数:

private void GetRigInfo()
{
    RigInfo = new string[9];
    var fileLocation = new string[2];

    // The problem is that there's no telling where the hddrigsite.ini will be 
    stored.  So, we have to find out where it is from the hddconfig.ini.
    Log("Locating rig info");

    // There's no telling if this will be on a 32 or 64 bit OS.  Check for both
    var rigInfoLocation = File.ReadAllLines(Environment.Is64BitOperatingSystem ?
                          @"C:\Program Files (x86)\HDD DrillView\hddconfig.ini" : 
                          @"C:\Program Files\HDD DrillView\hddconfig.ini");

    // This should get us the location of the rigsite info we need.
    foreach (var s in rigInfoLocation.Where(s => s.Contains("data_dir")))
    {
        fileLocation = s.Split('=');
    }

    RigInfo = File.ReadAllLines(fileLocation[1] + "\\hddrigsite.ini");

    Log("Rig info found");
}

现在,当我单步执行并到达Log()函数的最后一个,并将鼠标悬停在 上时RigInfo,Visual Studio 智能感知显示给我RigInfo{string[30]}。现在,我一直明白这= new string[9]将创建一个 9 元素数组。那么为什么允许有 30 个元素呢?当我运行程序时,这个数组没有任何错误或任何东西。事实上,它的工作方式正是我在整体方案中所需要的方式。感谢您在理解它的方式和原因方面提供的任何和所有帮助。还附上截图以获得更好的视觉帮助。

令人困惑的智能感知

4

7 回答 7

5

这里 :

RigInfo = File.ReadAllLines(fileLocation[1] + "\\hddrigsite.ini");

您正在为变量分配一个新值。在这种情况下,一个新的字符串 []。

于 2012-11-07T20:36:05.590 回答
4

因为您已在此行更改了存储在变量中的引用:

RigInfo = File.ReadAllLines(fileLocation[1] + "\\hddrigsite.ini");
于 2012-11-07T20:36:07.537 回答
3

你所做的是你用一个全新的数组重写了你的 9 元素数组

RigInfo = File.ReadAllLines(fileLocation[1] + "\\hddrigsite.ini");
于 2012-11-07T20:36:38.303 回答
2

该数组由 ReadAllLines 调用“重新定义”。如果您已通过索引将每一行分配给数组,那么您会得到一个错误,但在这种情况下,您将指针从分配给您的数组的内存中重定向,并将其指向 ReadAllLines 方法的输出。

总是对 Arr = somthing 感到厌烦,因为这会改变数组引用本身。

于 2012-11-07T20:37:21.653 回答
1

RigInfo contains more than the 9 elements expected because this line:

RigInfo = File.ReadAllLines(fileLocation[1] + "\hddrigsite.ini");

discards the original RigInfo and creates a new string array with the results of File.ReadAllLines(fileLocation[1] + "\hddrigsite.ini")

于 2012-11-07T20:40:46.743 回答
1

您分配File.ReadAllLines给它,因此将分配新内存并且该数组是一个全新的数组。你基本上覆盖了你以前的任务。

于 2012-11-07T20:36:53.807 回答
0

通过RigInfo = File.ReadAllLines(fileLocation[1] + "\\hddrigsite.ini");,您将new array结果从File.ReadAllLines(fileLocation[1] + "\\hddrigsite.ini");大小分配30RigInfo变量。

如果你这样做

  RigInfo [indx++] = one line at a time

那么它将在第 9 个元素之后失败,因为您使用的是先前定义的数组。

于 2012-11-07T20:36:36.420 回答