I cannot believe there is no smart way to get something like this from a 2D array,
in this case int[,] a:
"{1,2,3},{4,5,6},{7,8,9}"
I have read many similar questions and learned that string.Join() can only be used on jagged arrays (in 2D). But I don't want to use them because of the more complex initialization and because it just feels bad when my rows, which all have the same length, are spread over several places in memory.
This is my "normal" code:
var s = "";
for (int i = 0; i < a.GetLength(0); i++) {
if (i > 0) s += ',';
s += '{';
for (int j = 0; j < a.GetLength(1); j++) {
if (j > 0) s += ',';
s += a[i, j];
}
s += '}';
}
And here a "golfed" one:
var s = "{";
var i = 0;
foreach (var item in a) s += (i++ > 0 ? i % a.GetLength(1) == 1 ? "},{" : "," : "") + item;
s += '}';
:) - not really elegant, too, and the readability is more than bad.
Any suggestions? I'm open to Linq, since it doesn't have to be fast. I'm interested in improving elegance of the code, but not by just moving it to an extension method.