我可以通过一个参数使用强制转换来访问多维数组吗?

时间:2019-02-17 11:40:09

标签: c# multidimensional-array casting

我有一个3D数组和一个Point helper类。我可以以某种方式仅使用Point而不使用Point.x,Point.y,Point.z访问数组中的特定单元格吗?

conda create --name py3-ml python=3.6
conda install ipykernel

source activate py3-ml # or conda activate py3-ml
python -m ipykernel install --user --name py3-ml

有什么办法可以做到吗?我知道我可以在数组周围使用包装器。我可以用Point类做些什么吗?

谢谢!

1 个答案:

答案 0 :(得分:0)

如评论中所述,您不能完全按照您的要求进行操作。 但是,您可以通过向Point添加索引运算符来编写看起来一样不错的东西:

class Point
{
    int x, y, z;
    public Point(int _x, int _y, int _z)
    {
        x = _x; y = _y; z = _z;
    }

    public bool this[bool[,,] arr]
    {
        get { return arr[this.x, this.y, this.z]; }
        set { arr[this.x, this.y, this.z] = value; }
    }
}

然后,您可以通过以下方式分配True:

bool[,,] arr = new bool[10, 10, 10];
Point pt = new Point(5, 5, 5);

pt[arr] = true;

唯一的区别是,您必须执行arr[pt]而不是pt[arr]