gpt4 book ai didi

c# - 如何引用另一个类的 3D 数组?

转载 作者:行者123 更新时间:2023-11-30 23:25:57 28 4
gpt4 key购买 nike

在我的主程序类中:

 static void Main()
{
string[,,] anArray = new string [3,3,3];
anArray[0,0,0] = "value1";
anArray[0,0,1] = "value2"; .... //filling the rest of the array.
}

如何使用具有多个参数的构造函数将此数组传递给另一个单独的类“anotherClass”,例如:

 class AnotherClass
{
private string[,,] anotherClassArray;

public string[,,] AnotherClassArray
{
get { return anotherClassArray;}
}

public AnotherClass (string[,,] fromAnArray)
{
anotherClassArray = new string [fromAnArray.Length];
}
}

我看过一些示例,其中只有一个简单的一维数组从主程序传递到另一个单独的类,然后再返回,但是当我尝试按照相同的示例进行多维时,我得到了错误:

尝试初始化新数组时,“无法将类型 'string[]' 隐式转换为 'string[,,*]'”。

最佳答案

如果你想让 AnotherClass 拥有它自己的独立的、空的 3D 数组实例,那么你可以按照 Pikoh 所说的去做。在这种情况下,如果更改数组的内容,则在 Main 中创建的原始数组不受影响,反之亦然。

如果您希望 AnotherClass 引用与在 Main 中创建的数组相同的数组,因此可以访问它,填充内容,然后只需在 AnotherClass 构造函数中将 AnotherClass.anotherClassArray 引用设置为等于 fromAnArray ,如下所示:

public AnotherClass (string[,,] fromAnArray)
{
anotherClassArray = fromAnArray;
}

关于c# - 如何引用另一个类的 3D 数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37066279/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com