C#: Copy Section of 2D-Array

i'm working on a Client-Server Game with a tilebased map. If the user changes the visible section of the map (i.e. scrolls in any direction) i query the server for info about the new section giving X, Y, Width and Height.

The Map info is stored as 2D array


(MapTile is a simple Struct)

When the client requests a new section i want to read the section from the map tile array, for that i defined a method

public MapTile[,] GetMapSection(Rectangle area)
  [... snip validation ...]
  MapTile[,] result = new MapTile[area.Width, area.Height];

  for (Int32 y = 0; y < area.Height; ++y)
    for (Int32 x = 0; x < area.Width; ++x)
       result[x, y] = this.map[x + area.X, y + area.Y];
  return result;

Is there an easier (=> easier to read, understand and maintain, not necessarily faster) way to get the section from the Array?


Not really. This seemes like a sensible and simple way to do it. The reason .NET doesn't provide any built-in method is that, unlike with a 1D array, there are a whole variety of different ways of copying/combining multidimensional arrays.

You're not really going to get faster, and I think it's perfectly maintainable. Seems like pretty fixed code anyway.

I found a link on a MS example for arraycopy with c#

Array copy using pointers

Need Your Help

add values to cakephp layout from model app wide

php cakephp

thanks for reading, I know the question might sound fairly common, and well I wont deny the fact that maybe I'm just formulating what i want wrongly.