Im doing this:
string[,] string1 = {{"one", "0"},{"Two", "5"},{"Three","1"}};
int b = 0;
for(int i = 0; i <= string1.Length; i++)
{
b = int.Parse(string1[i, 1]); // Error occurs here
}
Im getting an error saying that “index extent the limits of the array” (or something like that, the error is in danish).
There are two problems:
string1.Lengthwill return 6, as that’s the total length of the array<=for the comparison, so it would actually try to iterate 7 times.Your
forloop should be:The call to
GetLength(0)here will return 3, as the size of the “first” dimension. A call toGetLength(1)return 2, as the second dimension is of size 2. (You don’t need that though, as you’re basically hard-coding the knowledge that you want the second “column” of each “row”.)See the docs for
Array.GetLength()for more details.