Hi
I have the following code example:-
class Program
{
static void Sum(params int[] ints)
{
int sum = 0;
for (int i = 0; i < ints.Length; i++)
{
sum += ints[i];
}
}
static void Main()
{
int[] ints = { 1, 2, 3, 4 };
Sum(ints);
Console.WriteLine(ints);
Console.ReadLine();
}
}
I was expecting the calling method "Main" to reflect the changes in the called method "Sum" as the array of ints is passed by reference?
Regards
Steven
Why doesnt this method call work?
2 Answers
Know the answer? Post it. Somebody with the same question will find it here.

VulpesRank #55 · 29.2K reputationPosted Aug 19, 2012, 11:39 AM EDT
However, there aren't any changes to the array's elements - they're just added up - and so the array remains unchanged when the method returns.
However, if you alter the program to this you will see the changes to the array persisted:
Guest UserRank #4,868 · 321 reputationPosted Aug 19, 2012, 12:02 PM EDT