简体   繁体   English

为什么数组中的元素多于我定义的元素?

[英]Why does the array have more elements than I've defined?

So, I've got this function which reads from an INI file: 所以,我有这个从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");
}

Now, when I step through, and get to the last Log() in the function, and I hover over RigInfo , Visual Studio intellisense shows me RigInfo{string[30]} . 现在,当我单步执行并到达函数中的最后一个Log()并将鼠标悬停在RigInfo ,Visual Studio intellisense会向我显示RigInfo{string[30]} Now, I've always understood that = new string[9] would create a 9 element array. 现在,我一直都明白= new string[9]会创建一个9元素数组。 So why is it allowed to have 30 elements? 那为什么它允许有30个元素呢? When I run the program, I get no errors or anything when it comes to this array. 当我运行程序时,在这个数组中我没有任何错误或任何错误。 Matter of fact, it works just the way that I need it to in the overall scheme of things. 事实上,它只是在整体方案中我需要它的方式。 Thanks for any and all help in understanding how and why this is the way it is. 感谢任何和所有帮助,了解它是如何以及为什么这样。 Also attached screenshot for better visual aid. 还附有截图,以获得更好的视觉辅助

令人困惑的智能感知

Here : 这里 :

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

You are assigning the variable with a new value.. In this case a new string[]. 您正在为变量分配一个新值。在这种情况下,新的字符串[]。

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

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

你做的是你用一个全新的数组覆盖你的9元素数组

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

the array is 'redefined' by the ReadAllLines call. 数组由ReadAllLines调用'重新定义'。 if you had assigned each line to the array by index then you woudl get an error, but in this case, you redirected your pointer from the memory allocated to your array, and pointed it to the output of the ReadAllLines method. 如果你已经通过索引将每一行分配给数组,那么你就会得到一个错误,但在这种情况下,你将指针从分配给你的数组的内存重定向,并将它指向ReadAllLines方法的输出。

always be weary of Arr = somthing, since that will alter the array reference itself. 总是厌倦了Arr = somthing,因为这会改变数组引用本身。

You assign File.ReadAllLines to it, so new memory will be allocated and the array is an entire new array. 您将File.ReadAllLines分配给它,因此将分配新内存并且该数组是一个完整的新数组。 You basically overwrite your previous assignment. 您基本上会覆盖以前的作业。

RigInfo contains more than the 9 elements expected because this line: RigInfo包含超过预期的9个元素,因为这一行:

RigInfo = File.ReadAllLines(fileLocation[1] + "\\hddrigsite.ini"); 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") 丢弃原始的RigInfo并创建一个新的字符串数组,其结果为File.ReadAllLines(fileLocation [1] +“\\ hddrigsite.ini”)

By RigInfo = File.ReadAllLines(fileLocation[1] + "\\\\hddrigsite.ini"); 通过RigInfo = File.ReadAllLines(fileLocation[1] + "\\\\hddrigsite.ini"); , you are assigning a new array resulting from File.ReadAllLines(fileLocation[1] + "\\\\hddrigsite.ini"); ,您正在分配一个由File.ReadAllLines(fileLocation[1] + "\\\\hddrigsite.ini");生成的new array File.ReadAllLines(fileLocation[1] + "\\\\hddrigsite.ini"); of size 30 to RigInfo variable. 大小为30RigInfo变量。

If you do 如果你这样做

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

then it will fail after 9th element as you are using the previously define array. 然后,当您使用先前定义的数组时,它将在第9个元素后失败。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么VB有比LIN#更多的LINQ关键字? - Why does VB have more LINQ keywords than C#? 为什么我允许传递比我为该方法定义的参数更多的参数? - Why am I allowed to pass in more parameters than I defined for the method? 删除出现多次的数组元素 - Remove array elements that appear more than once 当我有两个以上元素时,如何改进列表下的添加项目 - How improve add items under the List, when i have more than 2 elements 如果可以添加超过大小的元素,我们为什么要初始化数组的大小? - Why do we initialize the size of an array, if it is possible to add elements more than the size? 为什么一个类的数组比结构数组消耗的内存多20%? - Why does an array of classes consume ~20% more memory than array of structs? 我可以更快地找到比数组更多的索引吗? - Can I speed finding indexes of an array more than what I have? 我只是不能在 C# 中的数组中插入超过 5 个元素 - I just can't insert more than 5 elements in a array in C# 我有二维数组,如果一行中有 3 个(或更多)元素彼此相邻,我想返回 true, - I have 2 dimensional array and I want to returns true if there are 3 (or more) elements next to each other in one row, 为什么要使用二维以上的数组? - Why use an Array of more than two dimensions?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM