1

I have the following code in C#:

public class ELL
{
    public struct RVector
    {
        private int ndim;
        private double[] vector;

        public RVector(double[] vector) => (ndim, this.vector) = (vector.Length, vector);
        public double this[int i] { get => vector[i];  set => vector[i] = value; }

        public override string ToString()
        {
            string str = "(";

            for (int i = 0; i < ndim - 1; i++)
                str += vector[i].ToString() + ", ";

            str += vector[ndim - 1].ToString() + ")";
            return str;
        }
    }
    private static void SwapVectorEntries(RVector b, int m, int n)
    {
        double temp = b[m];
        b[m] = b[n];
        b[n] = temp;
    }
    public static void M(string[] args)
    {
        var a = new double[4] { 1, 2, 3, 4 };
        var b = new RVector(a);

        Console.WriteLine(b);
        SwapVectorEntries(b, 1, 2); // Why after this command, b will be changed?
        Console.WriteLine(b);
    }
}

In this program, i creates a struct RVector. After that, i use a method SwapVectorEntries which have a struct parameter. Because, Struct is a value type, so i think the method SwapVectorEntries will not change the struct parameter. But, in the program, after the command SwapVectorEntries(b, 1, 2);, b has changed. Please explain me about this. Thank you !

3
  • 1
    You are not modifying the structure, you are modifying the array. Arrays are not value types. Declaring a structure with a mutable reference type as a field is not a great idea. Commented Nov 5, 2015 at 10:22
  • Also, you can skip vector values initialization (in public RVector(int ndim) constructor): in c#, every item of the new created array will have it's default value (0.0, in your case using double). That's different from C/C++ Commented Nov 5, 2015 at 10:27
  • You can also avoid having a ndim member variable: you can replace it with vector.length every time you need it. Commented Nov 5, 2015 at 10:32

2 Answers 2

4

Problem is in this.You have an array wich is reference type.When you create your

double[] a = new double[4] { 1, 2, 3, 4 };
RVector b = new RVector(a);

you have two references to that array.After when you pass your object into the method,

SwapVectorEntries(b, 1, 2);

your object is copied,BUT your new object have the same reference to that array.Here your have only one array and many references to it.

enter image description here

Sign up to request clarification or add additional context in comments.

3 Comments

But, b is an instance of structure RVector. It 's created by using a constructor with an array-parameter. So, i think b is a value type.
Yes b is a value type.But when it copies as parameter,all it's fields also copies,and beacuse "double[] vector" is a reference,it's value(the address of the array) also copies .So you are having 2 different objects of RVector,but they have to vectors which refer to the same object.
Your answer is very clear. Now I has understood it. Thank you very much !
2

B itself is not passed as a reference, but the copy of b has a Reference to the same double[].

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.