How can I convert these Java code pieces to C#?

   public String getRawMaze() {
    StringBuilder sb = new StringBuilder();
    for (int[] row : maze) {
        sb.append(Arrays.toString(row) + "\n");
    }
    return sb.toString();
}

How can I convert this code to C#?
Maze is:

private int[][] maze;

>Solution :

Adapt to C# naming conventions (methods start with upper letter), replace Arrays.toString by string.Join and change the syntax for foreach loop:

public string GetRawMaze()
{
    StringBuilder sb = new StringBuilder();
    foreach (int[] row in maze)
    {
        sb.Append("[" + string.Join(", ",row) + "]\n");
    }
    return sb.ToString();
}

Online-demo: https://dotnetfiddle.net/IqFYhJ

Leave a Reply