I am attempting to iterate over a 2d string array but my foreach statement has a compile error that I dont understand.
What am I doing wrong in this simple example & how can I perform what I am trying to do?
string URL = PRODUCT_URL + "?";
string[,] a = {{"a","1"},{"b","2"}};
foreach (string[] param in a) // error cannot convert type string to string[]
{
URL += param[0] + "=" + param[1] + "&";
}
C# has two similar constructs, Arrays of arrays and multidimensional arrays. What you have here is a 2 dimensional array, so the loop you want is
If you wanted to go the array of arrays approach, you’d want:
for your declaration.
Internally, C# implements the multidimensional array as a normal array with a size equal to the product of the dimension (e.g. since your
ais 2×2, it would be a linear array of length 4). This way, the programmer can use a more convenient syntax for member access and initialization.