Printing an entire array in C #

I have a simple 2D array:

int[,] m = { {0,0,0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0,0,0} }; 

How can I print this in a text file or something else? I want to print the entire array to a file, not just the contents. For example, I do not want all zeros to be in the queue: I want to see

 {{0,0,0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0,0,0} }; 

in him.

+4
source share
2 answers

Just iterate over and make a conclusion. Sort of

 static string ArrayToString<T>(T[,] array) { StringBuilder builder = new StringBuilder("{"); for (int i = 0; i < array.GetLength(0); i++) { if (i != 0) builder.Append(","); builder.Append("{"); for (int j = 0; j < array.GetLength(1); j++) { if (j != 0) builder.Append(","); builder.Append(array[i, j]); } builder.Append("}"); } builder.Append("}"); return builder.ToString(); } 
+4
source

There is no standard way to get these brackets { , you have to put them through the code during iteration through your array and write them to a file

+3
source

Source: https://habr.com/ru/post/1307241/


All Articles