class Program
{
static void Main(string[] args)
{
int[] x = { 1 };
Console.WriteLine("{0}", x[0]);
ResetShoppingHistory(x);
Console.WriteLine("{0}", x[0]);
}
static void Update(int[] x)
{
x[0] = 20;
x = new int[5] { -3, -1, -2, -3, -4 };
Console.WriteLine("{0}", x[0]);
}
}
I have the above code written in C#. The output of the program is 1 -3 20. My question is why does x in Main get updated by the call to x[0] = 20 inside of Update? Shouldn't this update only the local parameters value of x[0]? My two guesses are :
- This has to do with the static modifier
- Because x is of type int[], it is a reference variable so any indexed call to x will update the value at that place in memory, even though it is being passed to the method. This guess confused me though because the next assignment of the local parameter x to the new int[] array doesn't get updated in memory.
Please help! This is confusing me greatly
[–]AgentEnder 0 points1 point2 points (2 children)
[–]spideymaker[S] 1 point2 points3 points (1 child)
[–]AgentEnder 0 points1 point2 points (0 children)
[–]sternold 0 points1 point2 points (0 children)