user12860256
user12860256

Reputation:

Passing array by value in c#

As far as I understand, the default type or argument passing in c# is by value. Therefore no statement is required. But when I try the run following code, my A matrix in Main is being modified by the operations done to dMatrixU in the Factorize() method of class Decomposition. I'm sure the problem is in the constructor of the Decomposition when I just assing A to dMatrixU, the reference of A is being assigned instead of the values. Therefore my question on how to avoid this, all I have found is how to pass the arguments by reference. Again, as I understand no modifier is needed for passing the argument by value. Where am I wrong?

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];
                    }
                }
            }
        }

    }
}

Upvotes: 0

Views: 1725

Answers (1)

Christopher
Christopher

Reputation: 9804

As far is i understand, the default type or argument passing in c# is by value.

Unfortunately it is a bit more complicated and also has some execptions:

Reference types like Decomposition you hand in by making a copy of the reference. Unfortunately that means both still reference the same instance in memory. So despite a copy operation, it is call-by-Reference.

With value types like Int or double and their aliases, usually a copy is made. I do not know of any case where it does not, but I was wrong on those things before. So they are call by value.

Finally String and a few other reference types are inmutable by design. That has the advantage that they behave kinda like value types in this area. You hand in a Reference, but the instance itself can not be changed. The code can only create a new instance in memory with a different value. So despite handing over literal references, it kinda works like call by value.

Your specific case

Arrays are very explicitly Reference types. Handing them into a function without side effects, requires proper cloning. If it is a array of reference types, the cloning must be deep.

In your case you have arrays of value types. If you want to avoid call-by-reference side effects, you those arrays must be cloned. However as double is a value type, this cloning can be shallow. No need for a deep clone.

Unlike Java there is not a dedicated Clone() Method. And I am not sure why exactly. However you can often use one Collection to initialize another through the constructor. Or they even have a function like Array.Copy(), as TheBatman pointed out.

Upvotes: 2

Related Questions