2

I do not understand why when I change "copy_of_digits" array then both "digits" and "parse_result" arrays change as well? I checked some online help about passing by reference and passing by value and it is written there that in C# passing by value is default and that you have to use "ref" to pass by reference. I think that what happens here is that arrays are passed by reference, not by value, but I fail to understand why and how to fix it. Any help would be appreciated!

namespace TestWithArrays
    {
        class Program
        {
            public static void Main()
            {
                Console.WriteLine("Please enter 2 digits:");
                string user_input = Console.ReadLine();
                int[] parse_result = Parse(user_input);
                int[] multiply_by_two_result = MultiplyByTwo(parse_result);
                Console.WriteLine("The End...");
                Console.ReadLine();
            }
            public static int[] Parse(string user_input)
            {
                int[] digits = new int [2];
                digits[0] = Int32.Parse(user_input.Substring(0,1));
                digits[1] = Int32.Parse(user_input.Substring(1,1));
                return digits;
            }
            public static int[] MultiplyByTwo(int[] digits)
            {
                int[] copy_of_digits = new int [2];
                copy_of_digits = digits;
                Console.WriteLine("´digits´ array before copy has been modified: " + string.Join(string.Empty, digits));
                copy_of_digits[0] = copy_of_digits[0] * 2;
                copy_of_digits[1] = copy_of_digits[1] * 2 ;
                Console.WriteLine("´digits´ array after copy has been modified: " + string.Join(string.Empty, digits));
                Console.WriteLine("´parse_result´ after copy has been modified: " + string.Join(string.Empty, digits));
                return copy_of_digits;
            }
        }
    }
1
  • Guess: copy_of_digits = digits only copies the address of the array to the other one. Commented Sep 23, 2013 at 10:01

4 Answers 4

7

Instead of assigning references

copy_of_digits = digits;

Copy all values from digits array to copy-of_digites array:

Array.Copy(digits, copy_of_digits, 2);

Otherwise you will have several references pointing to same items in memory.

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

Comments

1

it is written there that in C# passing by value is default and that you have to use "ref" to pass by reference.

That's right, but a thing that is being pass by value is the reference itself (because array is reference type in .NET).

That's why you have to copy your array. You would have to copy all array items if only it was an array of reference type objects.

Comments

1

Yes, C# defaults to passing by value; but you need to be aware of what you are passing by value: here, you are passing a reference by value. The reference is basically the address of the array. Whether you change digits[0] = ..., or assign digits to another variable, and then change copy_of_digits[0] = ..., you are still changing the same array. Importantly, here:

int[] copy_of_digits = new int [2];
copy_of_digits = digits;

That first new int [2] array is completely discarded here - it serves no purpose. And the second line doesn't copy the array - it only copies the reference and assigns it to a different variable.

Basically, if we ignore the redundant new int [2] - there is only 1 array in your question. It naturally follows that no matter where or how we change the contents, the changes are seen everywhere: it is, after all, all the same array.

I suspect what you actually want to do here is:

public static int[] MultiplyByTwo(int[] digits)
{
    int[] copy_of_digits = (int[])digits.Clone();
    ...

1 Comment

Thanks for a detailed answer and your suggestion with clone also works very well!
0

ref is used to pass a reference to the variable. that means the reference/value that the variable holds can also be changed.

now the value that the variable is holding can be of reference type or value type (http://msdn.microsoft.com/en-us/library/t63sy5hs.aspx). in case of a value type e.g. int/struct, a copy of the value is passed whereas for reference type, you still get a reference to the original object (in your case, reference to the original array) and so changing the members changes the members of the original object.

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.