简体   繁体   中英

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

So, I've got this function which reads from an INI file:

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]} . Now, I've always understood that = new string[9] would create a 9 element array. So why is it allowed to have 30 elements? 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. 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.

always be weary of Arr = somthing, since that will alter the array reference itself.

You assign File.ReadAllLines to it, so new memory will be allocated and the array is an entire new array. You basically overwrite your previous assignment.

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")

By RigInfo = File.ReadAllLines(fileLocation[1] + "\\\\hddrigsite.ini"); , you are assigning a new array resulting from File.ReadAllLines(fileLocation[1] + "\\\\hddrigsite.ini"); of size 30 to RigInfo variable.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM