use*_*550 17 c# arrays multidimensional-array
我使用此代码将一个2D数组复制到另一个2D数组:
Array.Copy(teamPerformance, 0,tempPerformance,0, teamPerformance.Length);
Run Code Online (Sandbox Code Playgroud)
但是,当我更改某些数据时,tempPerformance
这些更改也适用于teamPerformance
.
我应该怎么做来控制它?
Ser*_*gan 35
你需要克隆()
double[,] arr =
{
{1, 2},
{3, 4}
};
double[,] copy = arr.Clone() as double[,];
copy[0, 0] = 2;
//it really copies the values, not a shallow copy,
//after:
//arr[0,0] will be 1
//copy[0,0] will be 2
Run Code Online (Sandbox Code Playgroud)
das*_*ght 15
这是正确的:Array.Copy
执行浅拷贝,因此内部维度内的数组实例通过引用复制.您可以使用LINQ制作副本,如下所示:
var copy2d = orig2d.Select(a => a.ToArray()).ToArray();
Run Code Online (Sandbox Code Playgroud)