private void testFunc()
        {
            int[] arrayParam = new int[88];
            arrayParam[2] = 67;
            calledFunc(arrayParam);
            Console.WriteLine("testFunc Done");

        }
        private void calledFunc(int[] intArray)
        {
            Console.WriteLine(intArray[2]);
            //if new array declared, values not changed in testFunc
            intArray = new int[66];
            intArray[2] = 33;
            intArray[5] = 55;
        }

If calledFunc simply changes array values, the changes are passed back to the calling method. But if calledFunc allocates a new array, this is not passed back to the caller. How do I get a called method to have the ability to either change existing array elements, or pass back a new array and its new values?

Recommended Answers

All 5 Replies

static void Main(string[] args)
        {
            int[] array = new int[] { 1, 2, 3 };
            DoSomething(ref array);

            foreach (int value in array)
                Console.WriteLine(value);

            Console.Read();
        }

        static void DoSomething(ref int[] array)
        {
            array = new int[] { 2, 4, 6, 8, 10 };
        }

Just starting out with C#, thought (incorrectly) arrays were automatically passed as ref. Thanks!

Just starting out with C#, thought (incorrectly) arrays were automatically passed as ref. Thanks!

It's a subtle distinction, but there is a difference. Everything is passed by value unless you specify otherwise. However, when you are working with reference types such as arrays, the value you are passing is the address to whatever you're working with, which (in your case) was the array. You could modify the elements of the array and the calling function would be aware of those changes once control was returned because you were working at the same address as the calling function. However, if you point the address to another array (by allocating a new array or simply pointing at another existing array), then you have changed the value of your parameter, which is not something the calling function would follow. That's when you make a break.

I hope I did justice to the wording of that.

At any rate, if all you want to do is make changes to the elements of the arrays (or perhaps properties of other reference objects) you accept as parameters, pass them as you originally thought. But if you want to keep track of changes such as the creation of new objects entirely, pass by reference.

Thanks! I find it a little confusing but I think what you are saying is that since I didn't pass by ref, a local copy of arrayParam was created in calledFunc(), but only the starting address was copied, not the entire array. Any references to array elements in calledFunc therefore changed the original array, but when I tried to reset the address I was reseting a local copy of the address so it was not returned. I hope I got that right?

Think of it as the address of the array was passed, not the address of the address. Therefore changes to the array were persisted, but not changes to the value of the address (by allocating a new array).

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.