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
Loading
VulpesPosted Aug 19, 2012, 3:39 PM
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 UserPosted Aug 19, 2012, 4:02 PM