[英]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. 大小为
30
到RigInfo
变量。
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.