Search code examples
c#arraysint32

What do the second set of square brackets do in a 1D array?


The following code works but I can't seem to find an explanation for why it does.

        string[] rangeBounds = tempRange.Split(':');
        char lowerBoundLetter = rangeBounds[0][0];
        char upperBoundLetter = rangeBounds[1][0];

The variable tempRange is a string variable which holds a range of cell IDs such 'A6:B8'. How are A6 and B8 converted to A and B characters in the following lines? What is the use of the second sqaure brackets?


Solution

  • Strings are characters arrays.
    So your code first splits the incoming string (A6:B8) in two parts setting an array of strings (rangeBounds[0] = "A6" rangeBounds[1] = "B8")

    Then the line

    char lowerBoundLetter = rangeBounds[0][0];
    

    takes the first string (A6) in the rangeBounds array and using the second indexer takes the first character of that string (A). The second lines does the same thing but with the second string in the rangeBounds array