Home > Software engineering >  How can I convert these Java code pieces to C#?
How can I convert these Java code pieces to C#?

Time:05-14

   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;

What about in the second section? How can I convert it to C#?

 private void randomlyAddNodesToStack(ArrayList<Node> nodes) {
    int targetIndex;
    while (!nodes.isEmpty()) {
        targetIndex = rand.nextInt(nodes.size());
        stack.push(nodes.remove(targetIndex));
    }
}

CodePudding user response:

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

  • Related