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)?
There isn't the equivalent of a rectangular array as far as I'm aware, but you could:
ImmutableList<ImmutableList<Point>>
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];
}
}
}
See more on this question at Stackoverflow