Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Printing a singular array from within a 2D array in C#

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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)}]");
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading