I am writing code that entends to print out the first singular array as a whole from within a 2D array.
string[,] customerNames = new string [2,2] {{ "Bob", "Smith" },
{ "Sally", "Smith" }};
Console.WriteLine($"[{string.Join(", ",customerNames.GetValue(0)}]");
Expected output:
[Bob, Smith]
Current actual output:
Error: Specified file could not be executed.
Array was not a one-dimensional array.
The first element of the array is a one-dimensional array, however it is extracted from a larger 2D-array.
Is it possible to print out an array like this?
>Solution :
With multi-dimensional arrays, you’ll have to index the items individually to enumerate over a single rank/column. Get the number of the columns using GetLength(1) and get those values.
var rank = 0;
var customer = Enumerable.Range(0, customerNames.GetLength(1))
.Select(j => customerNames[rank, j]); // or customerNames.GetValue(rank, j)
Console.WriteLine($"[{string.Join(", ", customer)}]");