C# multidimensional immutable array

I need to create a field for simple game. In first version the field was like Point[,] - two dimensional array.

Now i need use System.Collections.Immutable (it's important condition). I trying to google and can't find anything, that can help me. I don't understand how i can create two-dimensional ImmutableArray (or ImmutableList)?

Jon Skeet
people
quotationmark

There isn't the equivalent of a rectangular array as far as I'm aware, but you could:

  • Have an ImmutableList<ImmutableList<Point>>
  • Wrap a single ImmutableList<Point> in your own class to provide access across two dimensions.

The latter would be something like:

// TODO: Implement interfaces if you want
public class ImmutableRectangularList<T>
{
    private readonly int Width { get; }
    private readonly int Height { get; }
    private readonly IImmutableList<T> list;

    public ImmutableRectangularList(IImmutableList<T> list, int width, int height)
    {
        // TODO: Validation of list != null, height >= 0, width >= 0
        if (list.Count != width * height)
        {
            throw new ArgumentException("...");
        }
        Width = width;
        Height = height;
        this.list = list;
    }

    public T this[int x, int y]
    {
        get
        {
            if (x < 0 || x >= width)
            {
                throw new ArgumentOutOfRangeException(...);
            }
            if (y < 0 || y >= height)
            {
                throw new ArgumentOutOfRangeException(...);
            }
            return list[y * width + x];
        }
    }
}

people

See more on this question at Stackoverflow