[英]Passing array by value in c#
据我了解,在c#
传递的默认类型或参数是按值传递的。 因此不需要声明。 但是当我尝试运行以下代码时,我在 Main 中的A
矩阵正在被类Decomposition
的Factorize()
方法中对dMatrixU
所做的操作修改。 我敢肯定,问题是在的构造Decomposition
时,我只是assing A
到dMatrixU
的参考A
被分配的,而不是价值。 因此,我关于如何避免这种情况的问题,我发现的只是如何通过引用传递参数。 同样,据我所知,按值传递参数不需要修饰符。 我哪里错了?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using LinearEquations;
namespace Rextester
{
public class Program
{
public static void Main(string[] args)
{
double[,] A = new double[,]
{ { 1, 1, 1 } ,
{ 4, 3, -1 } ,
{ 3, 5, 3 } };
double[] B = new double[] {1,6,4};
Decomposition lu = new Decomposition(A,B);
lu.Factorize();
PrintMatrix(A,"A:");
PrintVector(B,"B:");
PrintMatrix(lu.L,"L:");
PrintMatrix(lu.U,"U:");
PrintVector(lu.D,"D:");
}
public static void PrintMatrix(double[,] M, String Title = "Matrix: ")
{
Console.WriteLine(Title);
for(int i = 0; i<M.GetLength(0); i++)
{
for(int j = 0; j<M.GetLength(1);j++)
{
Console.Write(M[i,j]+"\t");
}
Console.Write("\n");
}
Console.Write("\n");
}
public static void PrintVector(double[] V, String Title = "Vector: ",bool AsRow = true)
{
String str = (AsRow)? "\t" : "\n";
Console.WriteLine(Title);
for(int i = 0; i<V.GetLength(0); i++)
{
Console.Write(V[i]+str);
}
Console.WriteLine("\n");
}
}
}
namespace LinearEquations
{
public class Decomposition
{
// Fields
private double[,] dMatrixA; // Parameter in A*X=B
private double[] dVectorB; // Parameter in A*X=B
private double[] dVectorX; // Result wanted in A*X=B
private double[,] dMatrixU; // A splits into L and U
private double[,] dMatrixL; // L is used to calculate D in L*D=B
private double [] dVectorD; // D is used to calculate X in U*X=D
// Properties
public double[,] A
{
get { return dMatrixA; }
set { dMatrixA = value; }
}
public double[] B
{
get { return dVectorB; }
set { dVectorB = value; }
}
public double[] X
{
get { return dVectorX; }
set { dVectorX = value; }
}
public double[,] L
{
get { return dMatrixL; }
set { dMatrixL = value; }
}
public double[,] U
{
get { return dMatrixU; }
set { dMatrixU = value; }
}
public double[] D
{
get { return dVectorD; }
set { dVectorD = value; }
}
// Constructor
public Decomposition(double[,] A, double[] B)
{
dMatrixA = A;
dVectorB = B;
dVectorX = new double[B.Length];
dMatrixU = A;
dMatrixL = new double[A.GetLength(0),A.GetLength(1)];
dVectorD = new double[B.Length];
}
// Split A into L and U
public void Factorize()
{
// Iterate per each row
for(int i = 0; i<dMatrixU.GetLength(0); i++)
{
// For all the rows make element i equals 0
for(int j = i+1; j<dMatrixU.GetLength(0);j++)
{
// Factor that assures substraction makes 0
dMatrixL[1,1] = dMatrixU[j,i] / dMatrixU[i,i];
// Iterate per each column
for(int k = 0; k<dMatrixU.GetLength(1);k++)
{
dMatrixU[j,k] = dMatrixU[j,k] - dMatrixU[i,k]*dMatrixL[1,1];
}
}
}
}
}
}
据我所知,在 c# 中传递的默认类型或参数是按值传递的。
不幸的是,它有点复杂,并且还有一些 execptions:
像Decomposition
这样的引用类型,您可以通过制作引用的副本来提交。 不幸的是,这意味着两者仍然在内存中引用同一个实例。 因此,尽管进行了复制操作,但它是按引用调用的。
对于像Int
或double
这样的值类型及其别名,通常会创建一个副本。 我不知道任何情况下没有,但我以前在这些事情上是错的。 所以它们是按值调用的。
最后, String
和其他一些引用类型在设计上是不可变的。 这样做的好处是它们在这方面的行为有点像值类型。 你交了一个引用,但实例本身不能改变。 代码只能在内存中创建一个具有不同值的新实例。 因此,尽管移交了字面引用,但它的工作方式有点像按值调用。
您的具体情况
数组是非常明确的引用类型。 将它们交给一个没有副作用的函数,需要适当的克隆。 如果是引用类型的数组,那么克隆一定是深的。
在您的情况下,您有值类型的数组。 如果要避免按引用调用的副作用,则必须克隆这些数组。 但是,由于 double 是值类型,因此这种克隆可能会很浅。 不需要深度克隆。
与 Java 不同,没有专用的 Clone() 方法。 我不确定为什么会这样。 但是,您通常可以通过构造函数使用一个集合来初始化另一个集合。 或者他们甚至有一个像Array.Copy()
这样的函数,正如 TheBatman 指出的那样。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.