C#:多维数组外观?

C#:多维数组外观?,c#,multidimensional-array,C#,Multidimensional Array,我有以下课程: public class Layer { public Tile[,] Grid; //50x50 positions filled with my own Tile struct } public class Level { public Layer[] Layers; //An array of layers in the level public List<object> Objects; //And a couple of lists

我有以下课程:

public class Layer
{
    public Tile[,] Grid; //50x50 positions filled with my own Tile struct
}

public class Level
{
    public Layer[] Layers; //An array of layers in the level
    public List<object> Objects; //And a couple of lists of say, characters or such in the level.
}

public class Area
{
    private Level[,] _activeLevels; //3x3 array of the current level and surrounding ones.
}
从标高[2,1]获取网格[12,14]中的瓷砖

更多说明:

假设一个层有50 x 50个位置,我希望下面能解释我想要的

“如果我打电话”=>“那么我真的想要”

--[下一部分已回答,见下文]--


还有,我想打电话给你,比如

Area.Layers[0].Grid[112, 64];
foreach (object o in Area.Objects)
      //dostuff
在九个级别的所有对象上调用foreach



谁能给我一个正确的方向,一些关于如何实现这一点的建议,或者干脆把它编码出来?

不确定第一个例子中你想要什么,你可能想检查它是否正确

通过实现自己的IEnumerable,可以在1迭代器中循环所有对象:

public class Area {
        private Level[,] _activeLevels; //3x3 array of the current level and surrounding ones.

        IEnumerable<object> Objects {
            get {
                foreach (Level lvl in _activeLevels)
                    foreach (object o in lvl.Objects)
                        yield return o;

            }
        }
    }
公共类区域{
私有级别[,]\u activeLevels;//当前级别和周围级别的3x3数组。
IEnumerable对象{
得到{
foreach(活动级别中的级别L)
foreach(层对象中的对象o)
收益率o;
}
}
}

同一对象能否在不同级别之间共享?在这种情况下,您可能需要筛选重复项。

对于
Area.Layers[0].Grid[112,64]
您最好使用
Area.GetTile(0,112,64)
的形式作为

public Tile GetTile(int layer, int overallX, int overallY)
{
    return _activeLevels[overallX / 50, overallY / 50]
            .Layers[layer]
            .Grid[overallX % 50, overallY % 50];
}

我听不懂你在说什么。你能以图画的形式给出一些想法吗?我寻求的是能够“自动”将“坐标”区域.Layer.Grid[112,64]转换为Level[3,2].Layer.Grid[12,14]。他们目前没有公开,因为我不知道如何达到这个结果。谢谢,这(部分)回答了我的问题。我没有想到能以这样的方式提供一个干劲。我想这会达到我想要的。谢谢你告诉我方法可能是更好的方法。你可以像你想要的那样设置你的类使用方括号符号,但是你必须专门创建一个或两个类来完成它;如果方法语法也是可以接受的,那么它肯定更简单。这种语法也是可以接受的。但是,用方括号怎么实现呢?如果有太多的麻烦,也不用麻烦,但是学习新事物总是好的。你想要的构造是一个索引器-请参阅。您将给
Area
一个名为
Layers
的属性,该属性将返回一个名为
FullLayerCollection
的新类。该类将有一个索引器返回另一个名为
FullLayer
的新类。这个类将有一个二维索引器返回一个
平铺
。此索引器必须回调父
区域
,本质上调用上面的
GetTile
方法,以确定要返回的
Tile
对象。。。。事实上,我错过了让你在第一组和第二组方括号之间键入
.Grid
所需的额外类。基本上,在这种情况下,我认为这将是比它的价值更多的麻烦。虽然索引器通常是有用的。
public Tile GetTile(int layer, int overallX, int overallY)
{
    return _activeLevels[overallX / 50, overallY / 50]
            .Layers[layer]
            .Grid[overallX % 50, overallY % 50];
}