Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# array values change unexpectedly in function

Tags:

arrays

c#

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;
            }
        }
    }
like image 556
Victor Avatar asked May 25 '26 15:05

Victor


2 Answers

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.

like image 117
Sergey Berezovskiy Avatar answered May 28 '26 15:05

Sergey Berezovskiy


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.

like image 42
MarcinJuraszek Avatar answered May 28 '26 15:05

MarcinJuraszek



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!