[英]C# multidimensional immutable array
我需要為簡單的游戲創建一個字段。 在第一個版本中,該字段類似於Point[,]
-二維數組。
現在我需要使用System.Collections.Immutable(這是重要條件)。 我嘗試使用Google卻找不到任何東西,對我有幫助。 我不明白如何創建二維ImmutableArray(或ImmutableList)?
據我所知,這不等同於矩形數組,但是您可以:
ImmutableList<ImmutableList<Point>>
ImmutableList<Point>
以提供跨兩個維度的訪問。 后者類似於:
// 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];
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.