-2

I have the following example for array modification

static void main()
{
    int[] arr1 = { 1, 4, 5 };
    Console.WriteLine("{0}", arr1[0]);      
    ModifyArray(arr1);
    Console.WriteLine("{0}", arr1[0]);
}

static void ModifyArray(int[] arr1)
{
    arr1[0] = 20;
    arr1 = new int[5] { -3, -1, -2, -3, -4 };
    Console.WriteLine("{0}", arr1[0]);
}

Why it prints

1,-3,20

instead of

1, -3, -3

?

Peyman Mohamadpour
  • 17,954
  • 24
  • 89
  • 100
kowsik
  • 21
  • 2
  • Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. Questions without a clear problem statement are not useful to other readers. See: How to create a Minimal, Complete, and Verifiable example. – rory.ap Mar 03 '17 at 13:03
  • Possible duplicate of [Passing Arrays by Value and by Reference](http://stackoverflow.com/questions/10325323/passing-arrays-by-value-and-by-reference) – ASh Mar 03 '17 at 13:09
  • In fairness, if you run the OPs code it will give the results that he indicated. The OP's misunderstanding is with the way that reference types such as arrays are passed by passing a copy of the reference *by value*. – Matthew Watson Mar 03 '17 at 13:13
  • Wrong close reason, it's a duplicate from [this](http://stackoverflow.com/questions/10325323/passing-arrays-by-value-and-by-reference) but it's not asking for debugging help because it is working. Just not as expected – Tim Schmelter Mar 03 '17 at 13:14
  • You're right, it's a duplicate. Can we fix that? (i.e. reopen it to close it as a dupe) – Matthew Watson Mar 03 '17 at 13:16
  • @MatthewWatson: sure, click reopen ;) – Tim Schmelter Mar 03 '17 at 14:01

1 Answers1

4

That program is behaving correctly.

A copy of the reference to arr1 is passed by value to ModifyArray(), so when you assign a new value to arr1 inside ModifyArray() it does not change the original array.

If you want to change the original array reference you must pass it by ref by using the ref keyword like so:

public static void Main()
{
    int[] arr1 = { 1, 4, 5 };
    Console.WriteLine("{0}", arr1[0]);
    ModifyArray(ref arr1);
    Console.WriteLine("{0}", arr1[0]);
}

static void ModifyArray(ref int[] arr1)
{
    arr1[0] = 20;
    arr1 = new int[5] { -3, -1, -2, -3, -4 };
    Console.WriteLine("{0}", arr1[0]);
}
Matthew Watson
  • 104,400
  • 10
  • 158
  • 276
  • Although now the first line in the method is useless because you assign a new array afterwards. – Tim Schmelter Mar 03 '17 at 13:12
  • @TimSchmelter Well from the point of view of the method itself it might do something, since you might have more than one reference to the array, and only the one passed to this method will be changed. For example, if just before calling `ModifyArray()` you did `var arr2 = arr1;` and then after the call you did `Console.WriteLine(arr2[0]);` - which would print `20`. – Matthew Watson Mar 03 '17 at 13:14